Skip to content

Commit 0f1afc9

Browse files
committed
Replace the pre-declartion with the parameter method
1 parent 853695f commit 0f1afc9

File tree

1 file changed

+19
-4
lines changed
  • Sprint-2/2-mandatory-debug

1 file changed

+19
-4
lines changed

Sprint-2/2-mandatory-debug/2.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Predict and explain first...
22

3-
// Predict the output of the following code:
4-
// =============> Write your prediction here
3+
// Predict the output of the following code
4+
// =============> Write your prediction here : The code did not set any parameter/s for the function getLastDigit. So it will always take the value of num which is 103 as a default value.
55

66
const num = 103;
77

@@ -14,11 +14,26 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1414
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1515

1616
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
17+
// =============> write the output here : The output should be as the return line (103."103".3).
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+
*/
22+
1823
// Explain why the output is the way it is
19-
// =============> write your explanation here
24+
// =============> write your explanation here : Since .toString() converts the number to a string. The slice(-1) method extracts the last character from that string..
25+
2026
// Finally, correct the code to fix the problem
2127
// =============> write your new code here
28+
function getLastDigit(num) {
29+
return num.toString().slice(-1);
30+
}
31+
32+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
33+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
34+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2235

2336
// This program should tell the user the last digit of each number.
2437
// Explain why getLastDigit is not working properly - correct the problem
38+
// >>>> because the pre declared value of num with const. So the function always returns the last digit of 103 which is 3.
39+
// >>>> getLastDigit should take a parameter to work properly.

0 commit comments

Comments
 (0)