|
3 | 3 | // Predict the output of the following code: |
4 | 4 | // =============> Write your prediction here |
5 | 5 |
|
6 | | -const num = 103; |
| 6 | +/** |
| 7 | + * The last digit of 42 input is '3' |
| 8 | + * The last digit of 105 input is '3' |
| 9 | + * The last digit of 806 input is '3' |
| 10 | + */ |
7 | 11 |
|
8 | | -function getLastDigit() { |
9 | | - return num.toString().slice(-1); |
10 | | -} |
| 12 | +/** |
| 13 | + * Original function: |
| 14 | + * |
| 15 | + * const num = 103; |
| 16 | + * |
| 17 | + * function getLastDigit() { |
| 18 | + * return num.toString().slice(-1); |
| 19 | + * } |
11 | 20 |
|
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)}`); |
| 21 | + * console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 22 | + * console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 23 | + * console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
| 24 | +*/ |
15 | 25 |
|
16 | 26 | // Now run the code and compare the output to your prediction |
17 | 27 | // =============> write the output here |
| 28 | + |
| 29 | +/** |
| 30 | + * The last digit of 42 input is '3' |
| 31 | + * The last digit of 105 input is '3' |
| 32 | + * The last digit of 806 input is '3' |
| 33 | + */ |
| 34 | + |
18 | 35 | // Explain why the output is the way it is |
19 | 36 | // =============> write your explanation here |
| 37 | + |
| 38 | +/** |
| 39 | + * Explanation: |
| 40 | + * |
| 41 | + * The function getLastDigit() is not working properly because: |
| 42 | + * It's ignoring the parameter: The function is defined to take a parameter, but when calling the function it's using the global variable num (which is set to 103) instead of the parameter passed to it. |
| 43 | + * There is no parameters in function definition. |
| 44 | + * The function is defined as function getLastDigit() without any parameters, so when we call getLastDigit(42), the 42 is ignored. |
| 45 | + * Fixed value: The function always returns the last digit of 103 (which is "3"), regardless of what number is passed to it. |
| 46 | + * That's why all three console logs show "3" - they're all getting the last digit of 103, not the numbers 42, 105, and 806 passed. |
| 47 | + */ |
| 48 | + |
20 | 49 | // Finally, correct the code to fix the problem |
21 | 50 | // =============> write your new code here |
22 | 51 |
|
| 52 | +const num = 103; |
| 53 | + |
| 54 | +function getLastDigit(number) { |
| 55 | + return number.toString().slice(-1); |
| 56 | +} |
| 57 | + |
| 58 | +console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 59 | +console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 60 | +console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
| 61 | + |
| 62 | +function getLastDigit(number) { |
| 63 | + return number.toString().slice(-1); |
| 64 | +} |
| 65 | + |
23 | 66 | // This program should tell the user the last digit of each number. |
24 | 67 | // Explain why getLastDigit is not working properly - correct the problem |
| 68 | + |
| 69 | +/** |
| 70 | + * The key fix was adding a parameter number to the function definition and using that parameter inside the function instead of the global num variable. Now each call to the function getLastDigit() works with the specific number passed to it. |
| 71 | + */ |
| 72 | + |
0 commit comments