Skip to content

Commit 54e11ce

Browse files
authored
Fix getLastDigit function to return correct last digit
Corrected the getLastDigit function to accept a parameter and return the last digit of the given number.
1 parent 7fbd07f commit 54e11ce

File tree

1 file changed

+15
-3
lines changed
  • Sprint-2/2-mandatory-debug

1 file changed

+15
-3
lines changed

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// =============>
5+
// The last digit of 42 is 3
6+
// The last digit of 105 is 3
7+
// The last digit of 806 is 3
58

69
const num = 103;
710

@@ -14,11 +17,20 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1417
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1518

1619
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
20+
// =============>// The last digit of 42 is 3
21+
// The last digit of 105 is 3
22+
// The last digit of 806 is 3
1823
// Explain why the output is the way it is
19-
// =============> write your explanation here
24+
// =============> the variable num is assigned to the value 103 in the global scope and not assigned to any value in the function scope
2025
// Finally, correct the code to fix the problem
2126
// =============> write your new code here
27+
function getLastDigit(num) { // we add the identier num as parameter to the function getLastDigit
28+
return num.toString().slice(-1);
29+
}
30+
31+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
32+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
33+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2234

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

0 commit comments

Comments
 (0)