|
1 | 1 | // Predict and explain first... |
| 2 | +// we want to get the last digit of the number value 103 |
2 | 3 |
|
3 | 4 | // Predict the output of the following code: |
| 5 | +//I think it will give us the wrong answer due to not specifying the actual result of what we want which is the lastDigit. |
| 6 | + |
4 | 7 | // =============> Write your prediction here |
5 | 8 |
|
6 | | -const num = 103; |
| 9 | +// const num = 103; |
7 | 10 |
|
8 | | -function getLastDigit() { |
9 | | - return num.toString().slice(-1); |
10 | | -} |
| 11 | +// function getLastDigit() { |
| 12 | +// return num.toString().slice(-1); |
| 13 | +// } |
11 | 14 |
|
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)}`); |
| 15 | +// console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 16 | +// console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 17 | +// console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
15 | 18 |
|
16 | 19 | // Now run the code and compare the output to your prediction |
| 20 | + |
| 21 | +//This is the answer that i received , which is different from my initial thought. |
| 22 | +//The last digit of 42 is 3 |
| 23 | +//The last digit of 105 is 3 |
| 24 | +//The last digit of 806 is 3 |
| 25 | + |
17 | 26 | // =============> write the output here |
18 | 27 | // Explain why the output is the way it is |
| 28 | +//the output is the same for all three due to the function always using the value 103. |
| 29 | + |
19 | 30 | // =============> write your explanation here |
| 31 | +// By creating a constant to the value of 103 the number will always calculate the const. and this is, is not what the function wants us to do, so we would have to remove it. |
| 32 | +//function -getLastDigit don't have a parameters which means that the other three values 42, 105, 806 will not be received by the function. |
| 33 | +//this means we need to give the function a parameter. |
| 34 | +//numToString changes the number into a string, this is so we can use the .slice() method. |
| 35 | +//.slice(-1) will give us the last character. |
| 36 | + |
20 | 37 | // Finally, correct the code to fix the problem |
21 | 38 | // =============> write your new code here |
22 | 39 |
|
| 40 | +function getLastDigit(num) { |
| 41 | + return num.toString().slice(-1); |
| 42 | +} |
| 43 | + |
| 44 | +console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 45 | +console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 46 | +console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
| 47 | + |
23 | 48 | // This program should tell the user the last digit of each number. |
24 | 49 | // Explain why getLastDigit is not working properly - correct the problem |
0 commit comments