|
2 | 2 |
|
3 | 3 | // Predict the output of the following code: |
4 | 4 | // =============> Write your prediction here |
5 | | -// II predict that the output will be "The last digit of 42 is 3", "The last digit of 105 is 3", |
| 5 | +// II predict that the output will be "The last digit of 42 is 3", "The last digit of 105 is 3", |
6 | 6 | // and "The last digit of 806 is 3" because the function getLastDigit is using the variable num |
7 | | -// which is assigned the value of 103 using const in global scope, |
| 7 | +// which is assigned the value of 103 using const in global scope, |
8 | 8 | // so it will always return the last digit of 103 which is 3. |
9 | 9 |
|
10 | 10 | // const num = 103; |
|
26 | 26 |
|
27 | 27 | // Explain why the output is the way it is |
28 | 28 | // =============> write your explanation here |
29 | | -// The output is the way it is because the function getLastDigit is using the variable num which is assigned |
| 29 | +// The output is the way it is because the function getLastDigit is using the variable num which is assigned |
30 | 30 | // the value of 103 using const in global scope, and the function definition didn't have an argument to receive the number. |
31 | 31 |
|
32 | 32 | // Finally, correct the code to fix the problem |
33 | 33 | // =============> write your new code here |
34 | 34 |
|
| 35 | +const num = 103; |
35 | 36 |
|
36 | | -function getLastDigit() { |
| 37 | +function getLastDigit(num) { |
37 | 38 | return num.toString().slice(-1); |
38 | 39 | } |
39 | 40 |
|
40 | 41 | console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
41 | 42 | console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
42 | 43 | console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
43 | | - |
0 commit comments