|
1 | 1 | // Predict and explain first... |
2 | 2 |
|
3 | | -// Predict the output of the following code: |
4 | | -// =============> Write your prediction here |
| 3 | +// Predict the output of the following code |
| 4 | +// =============> Write your prediction here: i think, the console.log will only show '3' because the paramater hasn't been specified in the function. |
5 | 5 |
|
6 | | -const num = 103; |
| 6 | +//const num = 103; |
7 | 7 |
|
8 | | -function getLastDigit() { |
| 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 | +// =============> write the output here: I've run the code and got '3' in each console.log line. |
| 18 | + |
| 19 | +// Explain why the output is the way it is |
| 20 | +// =============> write your explanation here: It's because there is no parameter in the function to differentiate viarable 103 from 42, 105, 806. |
| 21 | + |
| 22 | + |
| 23 | +// Finally, correct the code to fix the problem |
| 24 | +// =============> write your new code here |
| 25 | + |
| 26 | +function getLastDigit(num) { |
9 | 27 | return num.toString().slice(-1); |
10 | 28 | } |
11 | 29 |
|
12 | 30 | console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
13 | 31 | console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
14 | 32 | console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
15 | 33 |
|
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 |
| 34 | + |
| 35 | +// it works now, but since cosnt num = 103 is unused, i have removed it. |
22 | 36 |
|
23 | 37 | // This program should tell the user the last digit of each number. |
24 | 38 | // Explain why getLastDigit is not working properly - correct the problem |
0 commit comments