Skip to content

Commit b2530bc

Browse files
committed
Fixed getLastDigit() to use parameter instead of hardcoded value and added explanation
1 parent 11c30c6 commit b2530bc

File tree

1 file changed

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

1 file changed

+24
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// Prediction: The function will always return "3" because it uses the hardcoded value 103.
56

7+
/* Original code:
68
const num = 103;
79
810
function getLastDigit() {
@@ -12,13 +14,35 @@ function getLastDigit() {
1214
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1315
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1416
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
17+
*/
1518

1619
// Now run the code and compare the output to your prediction
1720
// =============> write the output here
21+
// The last digit of 42 is 3
22+
// The last digit of 105 is 3
23+
// The last digit of 806 is 3
24+
1825
// Explain why the output is the way it is
1926
// =============> write your explanation here
27+
// MDN Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions#function_parameters
28+
29+
// The function does not use the input number passed in.
30+
// It always returns the last digit of the variable `num`, which is 103.
31+
// To fix this, the function should accept a parameter and use that instead of the hardcoded value.
32+
2033
// Finally, correct the code to fix the problem
2134
// =============> write your new code here
35+
function getLastDigit(num) {
36+
return num.toString().slice(-1);
37+
}
38+
39+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
40+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
41+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2242

2343
// This program should tell the user the last digit of each number.
2444
// Explain why getLastDigit is not working properly - correct the problem
45+
46+
// The original function was not working as it wasn't taking any parameters and always used the variable `num = 103`.
47+
// So no matter what number you passed in, the function would ignore it and always returned the hardcoded value, "103"
48+
// instead, which was "3", in this case.

0 commit comments

Comments
 (0)