Skip to content

Commit b73f73f

Browse files
committed
In 2-mandatory-debug/2.js, adding a parameter in the function
1 parent b2f87c5 commit b73f73f

File tree

1 file changed

+21
-2
lines changed
  • Sprint-2/2-mandatory-debug

1 file changed

+21
-2
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,42 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// The output should always be 3 because the parameter is not defined in the function,
6+
// and the variable num is always 103 in the global scope.
57

68
const num = 103;
7-
9+
/*
810
function getLastDigit() {
911
return num.toString().slice(-1);
1012
}
1113
1214
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1315
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1416
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15-
17+
*/
1618
// Now run the code and compare the output to your prediction
1719
// =============> write the output here
20+
/*
21+
The last digit of 42 is 3
22+
The last digit of 105 is 3
23+
The last digit of 806 is 3
24+
*/
25+
// As expected, the output is always 3.
26+
1827
// Explain why the output is the way it is
1928
// =============> write your explanation here
29+
//The num is defined in the global scope as 103.
30+
// And the function does not accept any parameters.
31+
// So every time the function is called, it returns the last digit of 103, which is 3.
32+
2033
// Finally, correct the code to fix the problem
2134
// =============> write your new code here
35+
function getLastDigit(num) {
36+
return num.toString().slice(-1);
37+
}
38+
console.log(`The last digit of 42 is ${getLastDigit(42)}`); // should return 2
39+
console.log(`The last digit of 105 is ${getLastDigit(105)}`); // should return 5
40+
console.log(`The last digit of 806 is ${getLastDigit(806)}`); // should return 6
2241

2342
// This program should tell the user the last digit of each number.
2443
// Explain why getLastDigit is not working properly - correct the problem

0 commit comments

Comments
 (0)