|
1 | 1 | // Predict and explain first... |
2 | 2 |
|
3 | 3 | // Predict the output of the following code: |
4 | | -// =============> Write your prediction here |
| 4 | +// here we have a constant variable called num which stores the number 103 we then have declared the function getLastDigit which has an empty parameter we are then asking it to return the value stored inside num and apply the .toString method to it which turns the number 103 into a string "103" we are then taking "103" and applying the .slice method to it which will start from position -1 and return everything after it which leaves us with the string "3" then outside of the function, the function getLastDigit is being called 3 times in template literal using console.log with the arguments 42, 105 and 806 but because the variable num = "3" it will print the template literal with the "3" in the console when javascript gets to the function call for each value, which isn't the expected result. |
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 | +//console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
15 | 15 |
|
16 | 16 | // Now run the code and compare the output to your prediction |
17 | | -// =============> write the output here |
| 17 | +// output: The last digit of 42 is 3 |
| 18 | +//The last digit of 105 is 3 |
| 19 | +//The last digit of 806 is 3 |
18 | 20 | // Explain why the output is the way it is |
19 | | -// =============> write your explanation here |
| 21 | +// the console output prints this because the the methods have only been applied to the number stored in the num variable so javascript isn't able to access the numbers passed in when the function is called in the template literal as the function getLastDigit() has no parameter so it passes in the value stored in the num variable which is "3" and so every time the function is called "3" is returned. |
20 | 22 | // Finally, correct the code to fix the problem |
21 | | -// =============> write your new code here |
| 23 | +function getLastDigit(num) { |
| 24 | + let getLastDigitResult = num.toString().slice(-1); |
| 25 | + return getLastDigitResult; |
| 26 | +} |
| 27 | +console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 28 | +console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 29 | +console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
22 | 30 |
|
23 | 31 | // This program should tell the user the last digit of each number. |
24 | | -// Explain why getLastDigit is not working properly - correct the problem |
|
0 commit comments