|
2 | 2 |
|
3 | 3 | // Predict the output of the following code: |
4 | 4 | // =============> Write your prediction here |
5 | | -//the output will be: an error because the function getLastDigit is not defined to take any parameters, but we are trying to pass a number as an argument when we call the function. The error message will likely indicate that getLastDigit is not a function or that it cannot be called with arguments. |
6 | 5 |
|
7 | | -//const num = 103; |
| 6 | +const num = 103; |
8 | 7 |
|
9 | | -//function getLastDigit() { |
10 | | - //return num.toString().slice(-1); |
11 | | -//} |
| 8 | +function getLastDigit() { |
| 9 | + return num.toString().slice(-1); |
| 10 | +} |
12 | 11 |
|
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)}`); |
| 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)}`); |
16 | 15 |
|
17 | 16 | // Now run the code and compare the output to your prediction |
18 | 17 | // =============> write the output here |
19 | | -//it does run but returns the last digit of the number 103 for all three calls to getLastDigit, which is not the expected behavior. The output will be: |
20 | | -// The last digit of 42 is 3 |
21 | | -// The last digit of 105 is 3 |
22 | | -// The last digit of 806 is 3 |
23 | | - |
24 | 18 | // Explain why the output is the way it is |
25 | 19 | // =============> write your explanation here |
26 | | -// The function getLastDigit is not taking any parameters, so it always returns the last digit of the global variable 'num', which is 103. The function should take a number as a parameter and return the last digit of that number. |
27 | | - |
28 | 20 | // Finally, correct the code to fix the problem |
29 | 21 | // =============> write your new code here |
30 | | -function getLastDigit(num) { |
31 | | - return num.toString().slice(-1); |
32 | | -} |
33 | | -console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
34 | | -console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
35 | | -console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
36 | 22 |
|
37 | 23 | // This program should tell the user the last digit of each number. |
38 | 24 | // Explain why getLastDigit is not working properly - correct the problem |
0 commit comments