|
1 | 1 | // Predict and explain first... |
2 | 2 |
|
3 | 3 | // Predict the output of the following code: |
4 | | -// =============> |
| 4 | +// =============> The const num variable is assigned in the global scope and returned in the getLastDigit function. |
| 5 | +// The console logs lines 14-16 call the getLastDigit function due to this; the console will log "The last digit of 42 is 3", |
| 6 | +// "The last digit of 105 is 3", and |
| 7 | +// "The last digit of 806 is 3". |
5 | 8 |
|
6 | | -const num = 103; |
| 9 | +//const num = 103; |
7 | 10 |
|
8 | | -function getLastDigit() { |
| 11 | +//function getLastDigit() { |
| 12 | +//return num.toString().slice(-1); |
| 13 | +//} |
| 14 | + |
| 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)}`); |
| 18 | + |
| 19 | +// Now run the code and compare the output to your prediction |
| 20 | +// =============> The code performed as expected due to the declaration of num in the global and called in the function |
| 21 | +// =============> |
| 22 | +// Finally, correct the code to fix the problem |
| 23 | +// =============> write your new code here |
| 24 | + |
| 25 | +function getLastDigit(num) { |
9 | 26 | return num.toString().slice(-1); |
10 | 27 | } |
11 | 28 |
|
12 | 29 | console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
13 | 30 | console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
14 | 31 | console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
15 | 32 |
|
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 | 33 | // This program should tell the user the last digit of each number. |
24 | 34 | // Explain why getLastDigit is not working properly - correct the problem |
0 commit comments