Skip to content

Commit ff7893d

Browse files
committed
Fix getLastDigit() to use parameter instead of global variable
1 parent 59aa251 commit ff7893d

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

  • Sprint-2/2-mandatory-debug

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

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

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
//I think all three console.logs will output 3 because the function always uses the global variable.
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 last digit of 42 is 3
20+
The last digit of 105 is 3
21+
The last digit of 806 is 3*/
22+
1823
// Explain why the output is the way it is
1924
// =============> write your explanation here
25+
/*The output is the way it is because the variable num (103) is declared
26+
in the global scope. The function getLastDigit always uses this global variable
27+
instead of the argument passed in, so it always returns the last digit of 103, which is 3.*/
28+
2029
// Finally, correct the code to fix the problem
2130
// =============> write your new code here
31+
function getLastDigit(num) {
32+
return num.toString().slice(-1);
33+
}
34+
console.log(`The last digit of 103 is ${getLastDigit(103)}`);
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)}`);
2238

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

0 commit comments

Comments
 (0)