|
1 | | -// Predict and explain first... |
| 1 | +// Predict and explain first...: the function is supposed to return the last digit of a number, |
| 2 | +// but it won't because it's using a fixed number instead of a variable to get the last digit from. |
2 | 3 |
|
3 | 4 | // Predict the output of the following code: |
4 | | -// =============> Write your prediction here |
| 5 | +// =============> Write your prediction here : it will give the same last digits of the same number (3). |
5 | 6 |
|
6 | | -const num = 103; |
| 7 | +// const num = 103; |
7 | 8 |
|
8 | | -function getLastDigit() { |
9 | | - return num.toString().slice(-1); |
10 | | -} |
| 9 | +// function getLastDigit() { |
| 10 | +// return num.toString().slice(-1); |
| 11 | +// } |
11 | 12 |
|
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)}`); |
| 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)}`); |
15 | 16 |
|
16 | 17 | // 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 |
| 18 | +// =============> write the output here : It gave the same last digit for all the numbers, which is 3. |
| 19 | +// Explain why the output is the way it is : because the function is using a fixed number (103) . |
| 20 | +// =============> write your explanation here: because the function is using a fixed number (103) . |
20 | 21 | // Finally, correct the code to fix the problem |
21 | | -// =============> write your new code here |
| 22 | +// =============> write your new code here |
| 23 | + function getLastDigit(num) { |
| 24 | + return num.toString().slice(-1);} |
| 25 | + console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 26 | + console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 27 | + console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
22 | 28 |
|
23 | 29 | // This program should tell the user the last digit of each number. |
24 | 30 | // Explain why getLastDigit is not working properly - correct the problem |
0 commit comments