Skip to content

Commit 889a9a9

Browse files
committed
update: predict the output, explain the cause of the problem, then Fix getLastDigit function to correctly return the last digit of a number and update comments for clarity in Sprint-2/2-mandatory-debug/2.js
1 parent 6bc6fa9 commit 889a9a9

File tree

1 file changed

+20
-8
lines changed
  • Sprint-2/2-mandatory-debug

1 file changed

+20
-8
lines changed

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11
// Predict and explain first...
2+
// This code is intended to find the last digit of a number, but it has a bug.
23

34
// Predict the output of the following code:
5+
// Predict the output of the following code is '3' for the input 103. since the last digit of 103 is 3 and it is a global variable, so regardless of the input, it will always return the last digit of the global variable `num` which is 103.
46
// =============> Write your prediction here
57

6-
const num = 103;
8+
// const num = 103;
79

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
10+
// function getLastDigit() {
11+
// return num.toString().slice(-1);
12+
// }
1113

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)}`);
14+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
15+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
16+
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1517

1618
// Now run the code and compare the output to your prediction
19+
// The last digit of 42 is 3
20+
// The last digit of 105 is 3
21+
// The last digit of 806 is 3
22+
1723
// =============> write the output here
1824
// Explain why the output is the way it is
1925
// =============> write your explanation here
26+
// The output is always '3' because the function `getLastDigit` is using a global variable `num` which is set to 103
2027
// Finally, correct the code to fix the problem
2128
// =============> write your new code here
22-
29+
function getLastDigit(num) {
30+
return num.toString().slice(-1);
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)}`);
2335
// This program should tell the user the last digit of each number.
2436
// Explain why getLastDigit is not working properly - correct the problem

0 commit comments

Comments
 (0)