Skip to content

Commit 231d91e

Browse files
committed
Fix getLastDigit function to accept parameters and update predictions and explanations
1 parent 0c1f1ad commit 231d91e

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

  • Sprint-2/2-mandatory-debug

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

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

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
//I predict the code will output the number '3' for every function call.
56

67
const num = 103;
78

@@ -15,10 +16,30 @@ 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+
20+
//OUTPUT: '3' was the output for every function call, which is not what I predicted.
21+
1822
// Explain why the output is the way it is
1923
// =============> write your explanation here
24+
25+
// The output is the way it is because the first line of the code a const statement was declared with the value of 103,
26+
// and the function getLastDigit() is using that constant instead of the argument passed.
27+
2028
// Finally, correct the code to fix the problem
2129
// =============> write your new code here
2230

31+
function getLastDigit(num) {
32+
return num.toString().slice(-1);
33+
}
34+
35+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
36+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
37+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
38+
2339
// This program should tell the user the last digit of each number.
40+
2441
// Explain why getLastDigit is not working properly - correct the problem
42+
43+
// This function did not work because javascript looks for the variable inside it and as it was not declared in the function '()' it went looking for num in the global code
44+
// and locked onto the declared constant value of num = 103 instead of running the passed arguments individually.
45+
// the code as been corrected by removing the const num = 103 and added num as a parameter to the function getLastDigit(num) so it can now take the arguments individually and return the right output.

0 commit comments

Comments
 (0)