|
1 | | -// Predict and explain first... |
2 | | - |
3 | | -// Predict the output of the following code: |
4 | | -// =============> Write your prediction here |
5 | | -//The function has no parameter, so it cannot use input values like 42, 105, 806. |
6 | | - |
7 | | -const num = 103; |
8 | | - |
9 | | -function getLastDigit() { |
10 | | - return num.toString().slice(-1); |
11 | | -} |
12 | | - |
13 | | -console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
14 | | -console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
15 | | -console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
16 | | - |
17 | | -// Now run the code and compare the output to your prediction |
18 | | -// =============> write the output here |
19 | | -// Explain why the output is the way it is |
20 | | -// =============> write your explanation here |
21 | | -//The function is not using the value passed into it. |
22 | | -// Finally, correct the code to fix the problem |
23 | | -// =============> write your new code here |
24 | | -function getLastDigit(num) { |
25 | | - return num.toString().slice(-1); |
26 | | -} |
27 | | - |
28 | | -console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
29 | | -console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
30 | | -console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
31 | | -// This program should tell the user the last digit of each number. |
32 | | -// Explain why getLastDigit is not working properly - correct the problem |
33 | | -// If a function should work with different values → it must have parameters. |
34 | | -// Otherwise it will always use the same fixed value |
| 1 | +// Predict and explain first... |
| 2 | + |
| 3 | +// Predict the output of the following code: |
| 4 | +// =============> Write your prediction here |
| 5 | +//The function has no parameter, so it cannot use input values like 42, 105, 806. |
| 6 | + |
| 7 | +const num = 103; |
| 8 | + |
| 9 | +function getLastDigit() { |
| 10 | + return num.toString().slice(-1); |
| 11 | +} |
| 12 | + |
| 13 | +console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 14 | +console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 15 | +console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
| 16 | + |
| 17 | +// Now run the code and compare the output to your prediction |
| 18 | +// =============> write the output here |
| 19 | +// Explain why the output is the way it is |
| 20 | +// =============> write your explanation here |
| 21 | +//The function is not using the value passed into it. |
| 22 | +// Finally, correct the code to fix the problem |
| 23 | +// =============> write your new code here |
| 24 | +function getLastDigit(num) { |
| 25 | + return num.toString().slice(-1); |
| 26 | +} |
| 27 | + |
| 28 | +console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 29 | +console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 30 | +console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
| 31 | +// This program should tell the user the last digit of each number. |
| 32 | +// Explain why getLastDigit is not working properly - correct the problem |
| 33 | +// If a function should work with different values → it must have parameters. |
| 34 | +// Otherwise it will always use the same fixed value |
0 commit comments