Skip to content

Commit 5b73177

Browse files
committed
2.js committed
1 parent da968b7 commit 5b73177

File tree

1 file changed

+55
-7
lines changed
  • Sprint-2/2-mandatory-debug

1 file changed

+55
-7
lines changed

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

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

6-
const num = 103;
6+
/**
7+
* The last digit of 42 input is '3'
8+
* The last digit of 105 input is '3'
9+
* The last digit of 806 input is '3'
10+
*/
711

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
12+
/**
13+
* Original function:
14+
*
15+
* const num = 103;
16+
*
17+
* function getLastDigit() {
18+
* return num.toString().slice(-1);
19+
* }
1120
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)}`);
21+
* console.log(`The last digit of 42 is ${getLastDigit(42)}`);
22+
* console.log(`The last digit of 105 is ${getLastDigit(105)}`);
23+
* console.log(`The last digit of 806 is ${getLastDigit(806)}`);
24+
*/
1525

1626
// Now run the code and compare the output to your prediction
1727
// =============> write the output here
28+
29+
/**
30+
* The last digit of 42 input is '3'
31+
* The last digit of 105 input is '3'
32+
* The last digit of 806 input is '3'
33+
*/
34+
1835
// Explain why the output is the way it is
1936
// =============> write your explanation here
37+
38+
/**
39+
* Explanation:
40+
*
41+
* The function getLastDigit() is not working properly because:
42+
* It's ignoring the parameter: The function is defined to take a parameter, but when calling the function it's using the global variable num (which is set to 103) instead of the parameter passed to it.
43+
* There is no parameters in function definition.
44+
* The function is defined as function getLastDigit() without any parameters, so when we call getLastDigit(42), the 42 is ignored.
45+
* Fixed value: The function always returns the last digit of 103 (which is "3"), regardless of what number is passed to it.
46+
* That's why all three console logs show "3" - they're all getting the last digit of 103, not the numbers 42, 105, and 806 passed.
47+
*/
48+
2049
// Finally, correct the code to fix the problem
2150
// =============> write your new code here
2251

52+
const num = 103;
53+
54+
function getLastDigit(number) {
55+
return number.toString().slice(-1);
56+
}
57+
58+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
59+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
60+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
61+
62+
function getLastDigit(number) {
63+
return number.toString().slice(-1);
64+
}
65+
2366
// This program should tell the user the last digit of each number.
2467
// Explain why getLastDigit is not working properly - correct the problem
68+
69+
/**
70+
* The key fix was adding a parameter number to the function definition and using that parameter inside the function instead of the global num variable. Now each call to the function getLastDigit() works with the specific number passed to it.
71+
*/
72+

0 commit comments

Comments
 (0)