Skip to content

Commit 6986c8c

Browse files
authored
Fix getLastDigit function to return correct last digit
Corrected getLastDigit function to accept a parameter and return the last digit of the given number.
1 parent 5d7a4cf commit 6986c8c

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

  • Sprint-3/2-mandatory-debug

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// Output:
6+
// The last digit of 42 is 3
7+
// The last digit of 105 is 3
8+
// The last digit of 806 is 3
59

610
const num = 103;
711

@@ -15,10 +19,21 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1519

1620
// Now run the code and compare the output to your prediction
1721
// =============> write the output here
22+
// The last digit of 42 is 3
23+
// The last digit of 105 is 3
24+
// The last digit of 806 is 3
1825
// Explain why the output is the way it is
1926
// =============> write your explanation here
27+
// getLastDigit() does not have a parameter, so the values 42, 105 and 806 passed to it are not being used.
2028
// Finally, correct the code to fix the problem
2129
// =============> write your new code here
30+
function getLastDigit(num) {
31+
return num.toString().slice(-1);
32+
}
33+
34+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
35+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
36+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2237

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

0 commit comments

Comments
 (0)