Skip to content

Commit 6b1dc06

Browse files
Fix getLastDigit function to correctly return the last digit of a number
1 parent a79905f commit 6b1dc06

File tree

1 file changed

+19
-13
lines changed
  • Sprint-2/2-mandatory-debug

1 file changed

+19
-13
lines changed

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1-
// Predict and explain first...
1+
// Predict and explain first...: the function is supposed to return the last digit of a number,
2+
// but it won't because it's using a fixed number instead of a variable to get the last digit from.
23

34
// Predict the output of the following code:
4-
// =============> Write your prediction here
5+
// =============> Write your prediction here : it will give the same last digits of the same number (3).
56

6-
const num = 103;
7+
// const num = 103;
78

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
9+
// function getLastDigit() {
10+
// return num.toString().slice(-1);
11+
// }
1112

12-
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
13-
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14-
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
13+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
14+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
15+
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1516

1617
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
18-
// Explain why the output is the way it is
19-
// =============> write your explanation here
18+
// =============> write the output here : It gave the same last digit for all the numbers, which is 3.
19+
// Explain why the output is the way it is : because the function is using a fixed number (103) .
20+
// =============> write your explanation here: because the function is using a fixed number (103) .
2021
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
22+
// =============> write your new code here
23+
function getLastDigit(num) {
24+
return num.toString().slice(-1);}
25+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
26+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
27+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2228

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

0 commit comments

Comments
 (0)