|
1 | 1 | // Predict and explain first... |
2 | 2 |
|
3 | 3 | // Predict the output of the following code: |
4 | | -// =============> Write your prediction here |
5 | | - |
| 4 | +// =============> Write your prediction here: Function getLastDigit return num. |
| 5 | +/* |
6 | 6 | const num = 103; |
7 | 7 |
|
8 | 8 | function getLastDigit() { |
9 | 9 | return num.toString().slice(-1); |
| 10 | + |
10 | 11 | } |
11 | 12 |
|
12 | 13 | console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
13 | 14 | console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
14 | 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 | | -// =============> write the output here |
| 18 | +// =============> write the output here: output is last digit of variable num. |
18 | 19 | // Explain why the output is the way it is |
19 | | -// =============> write your explanation here |
| 20 | +// =============> write your explanation here: Because num is global and function's parameter is absent . |
20 | 21 | // Finally, correct the code to fix the problem |
21 | 22 | // =============> write your new code here |
| 23 | +const num = 103; |
| 24 | + |
| 25 | +function getLastDigit(num) { |
| 26 | + let digit = num.toString().slice(-1); |
| 27 | + return digit |
| 28 | +} |
| 29 | +console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 30 | +console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 31 | +console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
22 | 32 |
|
23 | 33 | // This program should tell the user the last digit of each number. |
24 | 34 | // Explain why getLastDigit is not working properly - correct the problem |
0 commit comments