|
1 | 1 | // Predict and explain first... |
2 | 2 |
|
3 | 3 | // Predict the output of the following code: |
4 | | -// =============> Write your prediction here |
| 4 | +// =============> Write your prediction here: We are trying to convert the last figure (last index) of a number to a String. We will surely get "2", "5" and "6" |
5 | 5 |
|
6 | | -const num = 103; |
| 6 | +// const num = 103; |
7 | 7 |
|
8 | | -function getLastDigit() { |
9 | | - return num.toString().slice(-1); |
10 | | -} |
| 8 | +// function getLastDigit() { |
| 9 | +// return num.toString().slice(-1); |
| 10 | +// } |
11 | 11 |
|
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)}`); |
| 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)}`); |
15 | 15 |
|
16 | 16 | // Now run the code and compare the output to your prediction |
17 | | -// =============> write the output here |
| 17 | +// =============> write the output here: After running our code, we saw that it printed "3" all over. Which comes from our initial declaration of num = 103 |
18 | 18 | // Explain why the output is the way it is |
19 | | -// =============> write your explanation here |
| 19 | +// =============> write your explanation here: We have declared num as a const which makes it unchangeable through out. We cannot redeclare it. The second thing is that we will need to add num inside of the parameter of the function. |
20 | 20 | // Finally, correct the code to fix the problem |
21 | 21 | // =============> write your new code here |
| 22 | +let num = 103; |
| 23 | + |
| 24 | +function getLastDigit(num) { |
| 25 | + return num.toString().slice(-1); |
| 26 | +} |
| 27 | + |
| 28 | +console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 29 | +console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 30 | +console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
22 | 31 |
|
23 | 32 | // This program should tell the user the last digit of each number. |
24 | 33 | // Explain why getLastDigit is not working properly - correct the problem |
0 commit comments