Skip to content

Commit 83072ae

Browse files
committed
sprint 3 ex3.2 solution
1 parent 857e12a commit 83072ae

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

  • Sprint-3/2-mandatory-debug

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Predict and explain first...
2-
2+
33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// =============> Write your prediction here : Even though the function calls pass in different numbers (42, 105, 806), the getLastDigit function doesn't take an input at all all three lines will print the same result: the last digit of 103, which is 3 — regardless of what number is passed in.
55

66
const num = 103;
77

@@ -15,10 +15,21 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1515

1616
// Now run the code and compare the output to your prediction
1717
// =============> write the output here
18+
:The last digit of 42 is 3
19+
The last digit of 105 is 3
20+
The last digit of 806 is 3
1821
// Explain why the output is the way it is
1922
// =============> write your explanation here
23+
: the arguments 42, 105, and 806 are being passed into getLastDigit(42) etc., but since the function is defined as function getLastDigit() — with empty parentheses — it has no parameters to receive those values. The passed-in arguments are simply discarded. inside the function isn't a parameter at all — it's referring to the completely separate outer num variable (103) via closure/scope, which is why every call gives the same answer regardless of input.
2024
// Finally, correct the code to fix the problem
2125
// =============> write your new code here
26+
function getLastDigit(num) {
27+
return num.toString().slice(-1);
28+
}
29+
30+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
31+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
32+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2233

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

0 commit comments

Comments
 (0)