|
1 | 1 | // Predict and explain first... |
2 | 2 |
|
3 | 3 | // Predict the output of the following code: |
4 | | -// =============> The code will return 3 as a string first, but after calling the getLastDigit function, |
5 | | -// it will not return anything because every number based will be converted to string. |
| 4 | +// =============> Write your prediction here |
6 | 5 |
|
7 | | -//const num = 103; |
| 6 | +const num = 103; |
8 | 7 |
|
9 | | -//function getLastDigit() { |
10 | | -// return num.toString().slice(-1); |
11 | | -//} |
12 | | - |
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)}`); |
16 | | - |
17 | | -// Now run the code and compare the output to your prediction |
18 | | -// =============> The last digit of 42 is 3 three times. |
19 | | -// the prediction was close but not accurate, the case is that the parameter num needs to be |
20 | | -//the function instead of const num = 103. |
21 | | -// =============> |
22 | | -// Finally, correct the code to fix the problem |
23 | | -// =============> write your new code here |
24 | | - |
25 | | -function getLastDigit(num) { |
| 8 | +function getLastDigit() { |
26 | 9 | return num.toString().slice(-1); |
27 | 10 | } |
28 | 11 |
|
29 | 12 | console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
30 | 13 | console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
31 | 14 | console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
| 15 | + |
| 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 | + |
32 | 23 | // This program should tell the user the last digit of each number. |
33 | 24 | // Explain why getLastDigit is not working properly - correct the problem |
34 | | -// parameter num needs to be with the getLastDigit function instead of const num=103. |
0 commit comments