|
2 | 2 |
|
3 | 3 | // Predict the output of the following code:
|
4 | 4 | // =============> Write your prediction here
|
| 5 | +// The output will be: |
| 6 | +// The last digit of 42 is 3 |
| 7 | +// The last digit of 105 is 3 |
| 8 | +// The last digit of 806 is 3 |
| 9 | +// Because getLastDigit always uses the global variable num (103) and ignores the argument. |
5 | 10 |
|
6 |
| -const num = 103; |
7 | 11 |
|
8 |
| -function getLastDigit() { |
9 |
| - return num.toString().slice(-1); |
10 |
| -} |
| 12 | +// const num = 103; |
11 | 13 |
|
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)}`); |
| 14 | +// function getLastDigit() { |
| 15 | +// return num.toString().slice(-1); |
| 16 | +// } |
| 17 | + |
| 18 | +// console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 19 | +// console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 20 | +// console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
15 | 21 |
|
16 |
| -// Now run the code and compare the output to your prediction |
| 22 | +// // Now run the code and compare the output to your prediction |
17 | 23 | // =============> write the output here
|
18 | 24 | // Explain why the output is the way it is
|
19 | 25 | // =============> write your explanation here
|
| 26 | +// The function getLastDigit does not accept any parameters and always uses the global variable num (103). |
| 27 | +// So, regardless of the argument passed, it always returns the last digit of 103, which is "3". |
| 28 | + |
20 | 29 | // Finally, correct the code to fix the problem
|
21 | 30 | // =============> write your new code here
|
22 |
| - |
| 31 | +function getLastDigit(num) { |
| 32 | + return num.toString().slice(-1); |
| 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)}`); |
23 | 37 | // This program should tell the user the last digit of each number.
|
24 | 38 | // Explain why getLastDigit is not working properly - correct the problem
|
0 commit comments