|
3 | 3 | // Predict the output of the following code: |
4 | 4 | // =============> Write your prediction here |
5 | 5 |
|
6 | | -const num = 103; |
| 6 | +// const num = 103; |
7 | 7 |
|
8 | | -function getLastDigit() { |
9 | | - return num.toString().slice(-1); |
10 | | -} |
| 8 | +// function getLastDigit() { |
| 9 | +// return num.toString().slice(-1); |
| 10 | +// } |
11 | 11 |
|
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)}`); |
| 12 | +// console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 13 | +// console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 14 | + |
| 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 | 18 | // =============> write the output here |
| 19 | +// the last digit of 42 is 3 |
| 20 | +// the last digit of 105 is 3 |
| 21 | +// the last digit of 806 is 3 |
18 | 22 | // Explain why the output is the way it is |
19 | 23 | // =============> write your explanation here |
| 24 | +// we will always get number 3 because num is 103 in the last digit of it is 3. |
| 25 | + |
20 | 26 | // Finally, correct the code to fix the problem |
21 | 27 | // =============> write your new code here |
| 28 | +const num = 103; |
| 29 | + |
| 30 | +function getLastDigit(num) { |
| 31 | + return num.toString().slice(-1); |
| 32 | +} |
| 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)}`); |
| 37 | + |
| 38 | + |
22 | 39 |
|
23 | 40 | // This program should tell the user the last digit of each number. |
24 | 41 | // Explain why getLastDigit is not working properly - correct the problem |
| 42 | +// We need num in the function as a parameter. |
0 commit comments