|
1 | 1 | // Predict and explain first... |
| 2 | +// In my idea we can not change value of num as it is defined by const |
2 | 3 |
|
3 | 4 | // Predict the output of the following code: |
4 | 5 | // =============> Write your prediction here |
| 6 | +//perhaps we will get value definition error |
5 | 7 |
|
6 | | -const num = 103; |
| 8 | +/*const num = 103; |
7 | 9 |
|
8 | 10 | function getLastDigit() { |
9 | 11 | return num.toString().slice(-1); |
10 | 12 | } |
11 | 13 |
|
12 | 14 | console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
13 | 15 | console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
14 | | -console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
| 16 | +console.log(`The last digit of 806 is ${getLastDigit(806)}`);*/ |
15 | 17 |
|
16 | 18 | // Now run the code and compare the output to your prediction |
17 | 19 | // =============> write the output here |
| 20 | +// The last digit of 42 is 3 |
| 21 | +//The last digit of 105 is 3 |
| 22 | +//The last digit of 806 is 3 |
| 23 | + |
18 | 24 | // Explain why the output is the way it is |
19 | | -// =============> write your explanation here |
| 25 | +// I was wrong in all the function call, the result is the same because the function always uses the global num. |
| 26 | + |
20 | 27 | // Finally, correct the code to fix the problem |
21 | | -// =============> write your new code here |
| 28 | +let num = 103; |
| 29 | + |
| 30 | +function getLastDigit(num) { |
| 31 | + return num.toString().slice(-1); |
| 32 | +} |
| 33 | + |
| 34 | +console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 35 | +console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 36 | +console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
22 | 37 |
|
23 | 38 | // This program should tell the user the last digit of each number. |
24 | 39 | // Explain why getLastDigit is not working properly - correct the problem |
0 commit comments