Skip to content

Commit d18dbb8

Browse files
Fixed shadowing issue and added corrected code
1 parent 5cc752a commit d18dbb8

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

  • Sprint-3/2-mandatory-debug

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// I predict every line will say the last digit is 3, because the function
66
// ignores the number I pass in and always uses num = 103.
77

8-
const num = 103;
8+
99

1010
function getLastDigit(num) {
1111
return num.toString().slice(-1);
@@ -22,12 +22,16 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2222
// The last digit of 806 is 3
2323
// Explain why the output is the way it is
2424
// =============> write your explanation here
25-
// getLastDigit has no parameter, so the argument in getLastDigit(42) is
26-
// ignored. Inside the function, num refers to the global constant
27-
// num = 103, so it always returns "3".
25+
// This is called shadowing. Originally, getLastDigit had no parameter, so
26+
// the argument (e.g., 42) was ignored. Inside the function, 'num' referred to
27+
// the global constant 'num = 103', which is why it always returned "3".
2828
// Finally, correct the code to fix the problem
2929
// =============> write your new code here
3030
// I added num as a parameter: function getLastDigit(num). Now num is the
3131
// value I pass in, not the global num = 103.
3232
// This program should tell the user the last digit of each number.
3333
// Explain why getLastDigit is not working properly - correct the problem
34+
35+
console.log(getLastDigit(42));
36+
console.log(getLastDigit(105));
37+
console.log(getLastDigit(806));

0 commit comments

Comments
 (0)