Skip to content

Commit 3ca2ab1

Browse files
committed
Fix getLastDigit function to accept a parameter and return the correct last digit
1 parent 1dca903 commit 3ca2ab1

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

  • Sprint-3/2-mandatory-debug

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
// Predict the output of the following code:
44
// =============> Write your prediction here
55

6+
// I predict that the output of the console.log statements will be "The last digit of 42 is 3",
7+
// "The last digit of 105 is 3", and "The last digit of 806 is 3" because the getLastDigit
8+
// function always returns the last digit of the number 103, which is 3.
9+
610
const num = 103;
711

812
function getLastDigit() {
@@ -15,10 +19,25 @@ 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
25+
1826
// Explain why the output is the way it is
1927
// =============> write your explanation here
28+
// The getLastDigit function is not working properly because it always returns the last
29+
// digit of the number 103, regardless of the input. This is because the function does not
30+
// take any parameters and uses a global variable.
31+
2032
// Finally, correct the code to fix the problem
2133
// =============> write your new code here
2234

35+
function getLastDigit(num) {
36+
return num.toString().slice(-1);
37+
}
38+
2339
// This program should tell the user the last digit of each number.
2440
// Explain why getLastDigit is not working properly - correct the problem
41+
// The getLastDigit function is not working properly because it does not take any parameters
42+
// and uses a global variable. To fix the problem, we need to add a parameter to the function
43+
// so that it can take in a number and return the last digit of that number.

0 commit comments

Comments
 (0)