|
2 | 2 |
|
3 | 3 | // Predict the output of the following code: |
4 | 4 | // =============> Write your prediction here |
| 5 | +// Prediction |
| 6 | +// When the function is called, I predict that the output will always be 3. |
| 7 | +// This is because the variable 'num' is defined outside the function and is always 103. |
| 8 | +// Therefore, regardless of the argument passed to getLastDigit, it will always return the last digit of 103, which is 3. |
5 | 9 |
|
6 | | -const num = 103; |
| 10 | + // const num = 103; |
7 | 11 |
|
8 | | -function getLastDigit() { |
9 | | - return num.toString().slice(-1); |
10 | | -} |
| 12 | +// function getLastDigit() { |
| 13 | + //return num.toString().slice(-1); |
| 14 | +//} |
11 | 15 |
|
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 | +//console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 17 | +//console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 18 | +//console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
15 | 19 |
|
16 | 20 | // Now run the code and compare the output to your prediction |
17 | 21 | // =============> write the output here |
| 22 | +// Output |
| 23 | +// The last digit of 42 is 3 |
| 24 | +// The last digit of 105 is 3 |
| 25 | +// The last digit of 806 is 3 |
| 26 | + |
18 | 27 | // Explain why the output is the way it is |
19 | 28 | // =============> write your explanation here |
| 29 | +// Explanation |
| 30 | +// The output will always be 3 because the function getLastDigit does not have a parameter so can not accept any arguments. |
| 31 | + |
20 | 32 | // Finally, correct the code to fix the problem |
21 | 33 | // =============> write your new code here |
| 34 | +// New code |
| 35 | +function getLastDigit(num) { |
| 36 | + return num.toString().slice(-1); |
| 37 | +} |
| 38 | + |
| 39 | +console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 40 | +console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 41 | +console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
22 | 42 |
|
23 | 43 | // This program should tell the user the last digit of each number. |
24 | 44 | // Explain why getLastDigit is not working properly - correct the problem |
0 commit comments