Skip to content

Commit 47e5c24

Browse files
Debug last digit function
1 parent d423953 commit 47e5c24

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

  • Sprint-3/2-mandatory-debug

‎Sprint-3/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 will be the same because we don't give the function a parameter
56

6-
const num = 103;
7+
/*const num = 103;
78
89
function getLastDigit() {
910
return num.toString().slice(-1);
1011
}
1112
1213
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1314
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14-
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15+
console.log(`The last digit of 806 is ${getLastDigit(806)}`); */
1516

1617
// Now run the code and compare the output to your prediction
1718
// =============> write the output here
19+
// The output is the same but the last digit is 3
20+
1821
// Explain why the output is the way it is
1922
// =============> write your explanation here
23+
// Because of the global variable and also because the function has no parameter
24+
25+
2026
// Finally, correct the code to fix the problem
2127
// =============> write your new code here
2228

29+
const num = 103;
30+
31+
function getLastDigit(num) {
32+
return num.toString().slice(-1);
33+
}
34+
35+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
36+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
37+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
38+
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
42+
43+
// Because the function was using the global variable, it ignored the arguments passed to it outside of the function box

0 commit comments

Comments
 (0)