|
1 | | -// Predict and explain first... |
2 | | - |
3 | | -// Predict the output of the following code: |
4 | | -// I predict the code will run but will give us unexpected output as the function does not have parameter but we provided an argument |
5 | | - |
6 | | -/*const num = 103; |
7 | | -
|
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 | | -// The last digit of 42 is 3 |
18 | | -// The last digit of 105 is 3 |
19 | | -// The last digit of 806 is 3 |
20 | | - |
21 | | -// Explain why the output is the way it is |
22 | | -// It is because we declared a function with no parameters so the function will ignore any arguments we provide |
23 | | -// since the var num is declared and assigned with a value so every time the function is called it will use the var num |
24 | | - |
25 | | -// Finally, correct the code to fix the problem |
26 | | -// Here is the fixed code |
27 | | -function getLastDigit(num) { |
28 | | - return num.toString().slice(-1); |
29 | | -} |
30 | | - |
31 | | -console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
32 | | -console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
33 | | -console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
34 | | - |
35 | | -// This program should tell the user the last digit of each number. |
36 | | -// Explain why getLastDigit is not working properly - correct the problem |
| 1 | +// Predict and explain first... |
| 2 | + |
| 3 | +// Predict the output of the following code: |
| 4 | +// I predict the code will run but will give us unexpected output as the function does not have parameter but we provided an argument |
| 5 | + |
| 6 | +/*const num = 103; |
| 7 | +
|
| 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 | +// The last digit of 42 is 3 |
| 18 | +// The last digit of 105 is 3 |
| 19 | +// The last digit of 806 is 3 |
| 20 | + |
| 21 | +// Explain why the output is the way it is |
| 22 | +// It is because we declared a function with no parameters so the function will ignore any arguments we provide |
| 23 | +// since the var num is declared and assigned with a value so every time the function is called it will use the var num |
| 24 | + |
| 25 | +// Finally, correct the code to fix the problem |
| 26 | +// Here is the fixed code |
| 27 | +function getLastDigit(num) { |
| 28 | + return num.toString().slice(-1); |
| 29 | +} |
| 30 | + |
| 31 | +console.log(`The last digit of 42 is ${getLastDigit(42)}`); |
| 32 | +console.log(`The last digit of 105 is ${getLastDigit(105)}`); |
| 33 | +console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
| 34 | + |
| 35 | +// This program should tell the user the last digit of each number. |
| 36 | +// Explain why getLastDigit is not working properly - correct the problem |
0 commit comments