Skip to content

Commit dda6132

Browse files
committed
Fix getLastDigit function to accept parameters and return the correct last digit
1 parent 3f7a1aa commit dda6132

File tree

1 file changed

+24
-8
lines changed
  • Sprint-2/2-mandatory-debug

1 file changed

+24
-8
lines changed

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,39 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
//the function has no parameter so I am not sure if it get the 42, 105 and 806 as input or it will always use the value of num which is 103,
6+
// but I know it change the number to string and get the last digit using slice method, because of the -1 index that means the last character.
7+
// const num = 103;
58

6-
const num = 103;
9+
// function getLastDigit() {
10+
// return num.toString().slice(-1);
11+
// }
712

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
11-
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
1718
// =============> write the output here
19+
/*The last digit of 42 is 3
20+
The last digit of 105 is 3
21+
The last digit of 806 is 3*/
22+
23+
1824
// Explain why the output is the way it is
1925
// =============> write your explanation here
26+
// It happens because, as I predicted, the function always considers 103 as its argument
27+
2028
// Finally, correct the code to fix the problem
2129
// =============> write your new code here
30+
const num = 103;
31+
32+
function getLastDigit(num) {
33+
return num.toString().slice(-1);
34+
}
2235

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)}`);
2339
// This program should tell the user the last digit of each number.
2440
// Explain why getLastDigit is not working properly - correct the problem

0 commit comments

Comments
 (0)