Skip to content

Commit 375a081

Browse files
committed
add parameter to getLastDigit
1 parent 9f7a9a0 commit 375a081

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

  • Sprint-3/2-mandatory-debug

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

Lines changed: 16 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+
// =============> I think the result prints will always be 3 as num will always be 103.
55

66
const num = 103;
77

@@ -14,11 +14,23 @@ 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+
// =============> The last digit of 42 is 3
18+
// The last digit of 105 is 3
19+
// The last digit of 806 is 3
1820
// Explain why the output is the way it is
19-
// =============> write your explanation here
21+
// =============> because the code is using the outer variable num which is always 103.
22+
// so every call is returning the last digit of 103.
2023
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
24+
// =============>
25+
function getLastDigit(num) {
26+
return num.toString().slice(-1);
27+
}
28+
29+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
30+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
31+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
32+
2233

2334
// This program should tell the user the last digit of each number.
2435
// Explain why getLastDigit is not working properly - correct the problem
36+
// because there was no parameter so the function has no way of receiving the numbers. so adding a parameter fixes it.

0 commit comments

Comments
 (0)