|
1 | | -// Predict and explain first... |
| 1 | +// Predict and explain first |
| 2 | +// As per the code output should be the last digits of the given number . |
2 | 3 |
|
3 | 4 | // Predict the output of the following code: |
4 | 5 | // =============> Write your prediction here |
| 6 | +// |
| 7 | +// The output always be 3 3 3 3 3 3 3 but i thought output should be the last digits. |
5 | 8 |
|
6 | | -const num = 103; |
| 9 | +// const num = 103; |
7 | 10 |
|
8 | | -function getLastDigit() { |
9 | | - return num.toString().slice(-1); |
10 | | -} |
| 11 | +// function getLastDigit() { |
| 12 | +// return num.toString().slice(-1); |
| 13 | +// } |
11 | 14 |
|
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 | +// 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)}`); |
15 | 18 |
|
16 | 19 | // Now run the code and compare the output to your prediction |
17 | 20 | // =============> write the output here |
| 21 | +// The last digit of 42 is 3 |
| 22 | +// The last digit of 105 is 3 |
| 23 | +// The last digit of 806 is 3 |
18 | 24 | // Explain why the output is the way it is |
19 | 25 | // =============> write your explanation here |
| 26 | +// In this variable num is constant and there is no any declaration in function name. Due to this reasons result always be 3. |
20 | 27 | // Finally, correct the code to fix the problem |
21 | 28 | // =============> write your new code here |
| 29 | +const num = 103; |
| 30 | + |
| 31 | +function getLastDigit(num) { |
| 32 | + return num.toString().slice(-1); |
| 33 | +} |
| 34 | + |
| 35 | +console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 36 | +console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 37 | +console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
| 38 | + |
22 | 39 |
|
23 | 40 | // This program should tell the user the last digit of each number. |
24 | 41 | // Explain why getLastDigit is not working properly - correct the problem |
| 42 | +// To solve this problems we need to write variable in function. |
0 commit comments