Skip to content

Commit b0260ef

Browse files
committed
Predict and explain bug. Fix by removing num declaration and adding num parameter to function getLastDigit
1 parent 818cc32 commit b0260ef

1 file changed

Lines changed: 25 additions & 8 deletions

File tree

  • Sprint-3/2-mandatory-debug

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,41 @@
11
// Predict and explain first...
2-
32
// Predict the output of the following code:
3+
//The last digit of 42 is 3
4+
//The last digit of 105 is 3
5+
//The last digit of 806 is 3
46
// =============> Write your prediction here
7+
//num is defined outside of the function, so has scope in the function getLastDigit.
8+
//the function doesn't take any arguments and uses num, so will always return 3, no matter what
9+
//is passed to the function because it will use num, which is set to 103
510

6-
const num = 103;
11+
//const num = 103;
712

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
13+
//function getLastDigit() {
14+
// return num.toString().slice(-1);
15+
//}
1116

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)}`);
17+
//console.log(`The last digit of 42 is ${getLastDigit(42)}`);
18+
//console.log(`The last digit of 105 is ${getLastDigit(105)}`);
19+
//console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1520

1621
// Now run the code and compare the output to your prediction
1722
// =============> write the output here
23+
//The last digit of 42 is 3
24+
//The last digit of 105 is 3
25+
//The last digit of 806 is 3
1826
// Explain why the output is the way it is
1927
// =============> write your explanation here
28+
// As I predicted, for the reason above
2029
// Finally, correct the code to fix the problem
2130
// =============> write your new code here
2231

32+
function getLastDigit(num) {
33+
return num.toString().slice(-1);
34+
}
35+
36+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
37+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
38+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
39+
2340
// This program should tell the user the last digit of each number.
2441
// Explain why getLastDigit is not working properly - correct the problem

0 commit comments

Comments
 (0)