|
2 | 2 |
|
3 | 3 | // Predict the output of the following code: |
4 | 4 | // =============> Write your prediction here |
| 5 | +// Martin response - I predict the return value will be the last digit of num returned as a string |
5 | 6 |
|
6 | 7 | const num = 103; |
7 | 8 |
|
8 | 9 | function getLastDigit() { |
9 | 10 | return num.toString().slice(-1); |
10 | 11 | } |
11 | 12 |
|
12 | | -console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
13 | | -console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
14 | | -console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
| 13 | +// console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 14 | +// console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 15 | +// console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
15 | 16 |
|
16 | 17 | // Now run the code and compare the output to your prediction |
17 | 18 | // =============> write the output here |
18 | 19 | // Explain why the output is the way it is |
| 20 | +// Martin response - the output is always 3. The function does not take a parameter and the variable declared in the global scope, num, is called repeatedly inside the function. |
| 21 | + |
19 | 22 | // =============> write your explanation here |
| 23 | +// Martin response - The function does not take a parameter, although the function calls include a parameter. The variable in the global scope, num, is being called repeatedly within the function and this is why all function calls return the string value of 3 |
| 24 | + |
20 | 25 | // Finally, correct the code to fix the problem |
21 | 26 | // =============> write your new code here |
22 | 27 |
|
23 | 28 | // This program should tell the user the last digit of each number. |
24 | 29 | // Explain why getLastDigit is not working properly - correct the problem |
| 30 | +// Martin response - my explanation is above |
| 31 | + |
| 32 | +function getLastDigit(num) { |
| 33 | + return num.toString().slice(-1); |
| 34 | +} |
| 35 | + |
| 36 | +console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 37 | +console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 38 | +console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
0 commit comments