|
2 | 2 |
|
3 | 3 | // Predict the output of the following code: |
4 | 4 | // =============> Write your prediction here |
5 | | - |
6 | | -const num = 103; |
7 | | - |
8 | | -function getLastDigit() { |
9 | | - return num.toString().slice(-1); |
10 | | -} |
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)}`); |
| 5 | +/* we have a variable that is declared in the global scope, and in the function we are returning the variable -num, we also |
| 6 | + do not have a parameter for the function, but we call the function passing it an argument. we might get an argument error |
| 7 | +* |
| 8 | +* |
| 9 | +* */ |
| 10 | +// const num = 103; |
| 11 | +// |
| 12 | +// function getLastDigit() { |
| 13 | +// return num.toString().slice(-1); |
| 14 | +// } |
| 15 | +// |
| 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 | 21 | // =============> write the output here |
| 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 | + |
18 | 26 | // Explain why the output is the way it is |
19 | 27 | // =============> write your explanation here |
| 28 | +/* |
| 29 | +* The function does not have a parameter, so the arguments are ignored and the function instead uses the global variable |
| 30 | +* */ |
20 | 31 | // Finally, correct the code to fix the problem |
21 | 32 | // =============> write your new code here |
22 | 33 |
|
| 34 | +function getLastDigit(num) { |
| 35 | + return num.toString().slice(-1); |
| 36 | +} |
| 37 | +console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 38 | +console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 39 | +console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
| 40 | + |
23 | 41 | // This program should tell the user the last digit of each number. |
24 | 42 | // Explain why getLastDigit is not working properly - correct the problem |
0 commit comments