|
1 | 1 | // Predict and explain first... |
| 2 | +// The function intends to take a number and return it's last digit |
| 3 | +// Each call of the function will return the same result |
2 | 4 |
|
3 | 5 | // Predict the output of the following code: |
4 | | -// =============> Write your prediction here |
| 6 | +// 'The last digit of 42 is 3' |
| 7 | +// 'The last digit of 105 is 3' |
| 8 | +// 'The last digit of 806 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 | | -// =============> write the output here |
| 21 | +// 'The last digit of 42 is 3' |
| 22 | +// 'The last digit of 105 is 3' |
| 23 | +// 'The last digit of 806 is 3' |
| 24 | + |
18 | 25 | // Explain why the output is the way it is |
19 | | -// =============> write your explanation here |
| 26 | +// Because the function was not correctly defined to accept an argument, It was using a fixed variable instead |
| 27 | + |
20 | 28 | // Finally, correct the code to fix the problem |
21 | | -// =============> write your new code here |
| 29 | +function getLastDigit(num) { |
| 30 | + return num.toString().slice(-1); |
| 31 | +} |
22 | 32 |
|
23 | | -// This program should tell the user the last digit of each number. |
24 | | -// Explain why getLastDigit is not working properly - correct the problem |
| 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)}`); |
0 commit comments