Skip to content

Commit 35c7d91

Browse files
committed
Fix getLastDigit function parameter
1 parent d1a35bc commit 35c7d91

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

  • Sprint-3/2-mandatory-debug

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Predict and explain first...
22

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// =============> Write your prediction here:I predict the output will be 3 every time, because the function uses the global constant num = 103 instead of using the numbers passed into getLastDigit().
55

66
const num = 103;
77

@@ -14,11 +14,28 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1414
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1515

1616
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
17+
// =============> write the output here:
18+
// The last digit of 42 is 3
19+
//The last digit of 105 is 3
20+
//The last digit of 806 is 3
1821
// Explain why the output is the way it is
19-
// =============> write your explanation here
22+
// =============> write your explanation here:there is no parameter in the function.
23+
//So when we call the function:
24+
//getLastDigit(42)
25+
//the 42 is passed to the function, but the function has no parameter to receive it.
26+
//Instead, it uses the global variable (const num = 103)
27+
//so every call converts 103 to a string and gets its last digit: 3.
2028
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
29+
// =============> write your new code here:
30+
const num = 103;
31+
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)}`);
2239

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)