|
2 | 2 |
|
3 | 3 | // Predict the output of the following code: |
4 | 4 | // =============> Write your prediction here |
| 5 | + //getting the last digit of 103 |
| 6 | +// const num = 103; |
5 | 7 |
|
6 | | -const num = 103; |
7 | | - |
8 | | -function getLastDigit() { |
9 | | - return num.toString().slice(-1); |
10 | | -} |
| 8 | +// function getLastDigit() { |
| 9 | +// return num.toString().slice(-1); |
| 10 | +// } |
11 | 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)}`); |
| 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 | 15 |
|
16 | 16 | // Now run the code and compare the output to your prediction |
17 | 17 | // =============> write the output here |
| 18 | + //-The last digit of 42 is 3 |
| 19 | + //-The last digit of 105 is 3 |
| 20 | + //-The last digit of 806 is 3 |
| 21 | + |
18 | 22 | // Explain why the output is the way it is |
| 23 | + // -Every time the function calls 103 |
19 | 24 | // =============> write your explanation here |
20 | 25 | // Finally, correct the code to fix the problem |
21 | 26 | // =============> write your new code here |
22 | 27 |
|
23 | 28 | // This program should tell the user the last digit of each number. |
24 | 29 | // Explain why getLastDigit is not working properly - correct the problem |
| 30 | + //The function was looking at the outside "num" but Now there are two (num) one outside the scope "const num= 103" and i added another inside the function scope. |
| 31 | + //The function calculates something and returns it then console.log outside receives that returned value and prints. it. |
| 32 | +const num = 103; |
| 33 | + |
| 34 | +function getLastDigit() { |
| 35 | + return num.toString().slice(-1); |
| 36 | + |
| 37 | +// console.log(getLastDigit(42)); |
| 38 | +} |
| 39 | +console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 40 | +console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 41 | +console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
| 42 | + |
| 43 | + |
0 commit comments