|
1 | 1 | // Predict and explain first... |
2 | 2 |
|
3 | 3 | // Predict the output of the following code: |
4 | | -// =============> Write your prediction here |
| 4 | +// =============> The output will always be 3 for all cases. |
5 | 5 |
|
| 6 | + |
| 7 | +// Now run the code and compare the output to your prediction |
| 8 | +// =============> write the output here |
| 9 | +// The last digit of 42 is 3 |
| 10 | +//The last digit of 105 is 3 |
| 11 | +//The last digit of 806 is 3 |
| 12 | +// Explain why the output is the way it is |
| 13 | +// =============> write your explanation here |
| 14 | +//The function does not have a parameter. |
| 15 | +//It does not use the values 42, 105, or 806. |
| 16 | +//It always uses the global variable: |
| 17 | +// Finally, correct the code to fix the problem |
| 18 | +// =============> write your new code here |
6 | 19 | const num = 103; |
7 | 20 |
|
8 | | -function getLastDigit() { |
9 | | - return num.toString().slice(-1); |
| 21 | +function getLastDigit(value) { |
| 22 | + return value.toString().slice(-1); |
10 | 23 | } |
11 | 24 |
|
12 | 25 | console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
13 | 26 | console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
14 | 27 | console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
15 | 28 |
|
16 | | -// Now run the code and compare the output to your prediction |
17 | | -// =============> write the output here |
18 | | -// Explain why the output is the way it is |
19 | | -// =============> write your explanation here |
20 | | -// Finally, correct the code to fix the problem |
21 | | -// =============> write your new code here |
22 | 29 |
|
23 | 30 | // This program should tell the user the last digit of each number. |
24 | 31 | // Explain why getLastDigit is not working properly - correct the problem |
0 commit comments