|
2 | 2 |
|
3 | 3 | // Predict the output of the following code: |
4 | 4 | // =============> Write your prediction here |
| 5 | +// (A) The output will be "The last digit of... is 3" for all these due the the variable `num` being set to 103 and the function `getLastDigit` |
5 | 6 |
|
6 | | -const num = 103; |
| 7 | + |
| 8 | +/*const num = 103; |
7 | 9 |
|
8 | 10 | function getLastDigit() { |
9 | 11 | return num.toString().slice(-1); |
10 | 12 | } |
11 | 13 |
|
12 | 14 | console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
13 | 15 | 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 806 is ${getLastDigit(806)}`);*/ |
15 | 17 |
|
16 | 18 | // Now run the code and compare the output to your prediction |
17 | 19 | // =============> write the output here |
| 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 | 25 | // Explain why the output is the way it is |
19 | | -// =============> write your explanation here |
| 26 | +//(A) As predicted the output resunts last digit of the variable `num`, which is set to 103. Therefore, regardless of the input values (42, 105, 806), the function always returns '3'. |
| 27 | + |
20 | 28 | // Finally, correct the code to fix the problem |
21 | 29 | // =============> write your new code here |
22 | 30 |
|
| 31 | +function getLastDigit(num) { |
| 32 | + return num.toString().slice(-1); |
| 33 | +} |
| 34 | + |
| 35 | +console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 36 | +console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 37 | +console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + |
23 | 42 | // This program should tell the user the last digit of each number. |
24 | 43 | // Explain why getLastDigit is not working properly - correct the problem |
0 commit comments