|
1 | 1 | // Predict and explain first... |
2 | 2 |
|
3 | 3 | // Predict the output of the following code: |
4 | | -// =============> Write your prediction here |
| 4 | +// =============> first it will convert the number to string then it will slice the last character that function |
| 5 | +//then will print |
| 6 | +//The last digit of 42 os 2 |
| 7 | +//The last digit of 105 os 5 |
| 8 | +//The last digit of 806 os 6 |
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 |
18 | 24 | // Explain why the output is the way it is |
19 | | -// =============> write your explanation here |
| 25 | +// =============> Because the function always takes the global variable witch is 103 |
| 26 | +//the output will be the same witch 3. |
20 | 27 | // Finally, correct the code to fix the problem |
21 | | -// =============> write your new code here |
| 28 | +// =============> write your new code her |
| 29 | + |
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)}`); |
23 | 38 | // This program should tell the user the last digit of each number. |
24 | 39 | // Explain why getLastDigit is not working properly - correct the problem |
0 commit comments