|
1 | 1 | // Predict and explain first... |
2 | 2 |
|
3 | 3 | // Predict the output of the following code: |
4 | | -// =============> Write your prediction here |
| 4 | +// Because the num variable is defined globally and the function doesn't set the parameter using num, which isn't being called and the consol.log ignores it and refers back to the global variable being set on line 6 |
5 | 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)}`); |
| 15 | + |
| 16 | +// Now run the code and compare the output to your prediction |
| 17 | +// The last digit of 42 is 3 |
| 18 | +// The last digit of 105 is 3 |
| 19 | +// The last digit of 806 is 3 |
| 20 | + |
| 21 | +// Explain why the output is the way it is |
| 22 | +// There is no parameter set in the function, so the function when called is ignored and the console.log only outputs the const variable of num |
| 23 | + |
| 24 | +// Finally, correct the code to fix the problem |
6 | 25 | const num = 103; |
7 | 26 |
|
8 | | -function getLastDigit() { |
| 27 | +function getLastDigit(num) { |
9 | 28 | return num.toString().slice(-1); |
10 | 29 | } |
11 | 30 |
|
12 | 31 | console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
13 | 32 | console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
14 | 33 | console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
15 | 34 |
|
16 | | -// Now run the code and compare the output to your prediction |
17 | | -// =============> write the output here |
18 | | -// Explain why the output is the way it is |
19 | | -// =============> write your explanation here |
20 | | -// Finally, correct the code to fix the problem |
21 | | -// =============> write your new code here |
22 | | - |
23 | 35 | // This program should tell the user the last digit of each number. |
24 | 36 | // Explain why getLastDigit is not working properly - correct the problem |
0 commit comments