You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sprint-3/2-mandatory-debug/2.js
+13-2Lines changed: 13 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
// Predict and explain first...
2
-
2
+
3
3
// 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.
5
5
6
6
constnum=103;
7
7
@@ -15,10 +15,21 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15
15
16
16
// Now run the code and compare the output to your prediction
17
17
// =============> write the output here
18
+
:Thelastdigitof42is3
19
+
Thelastdigitof105is3
20
+
Thelastdigitof806is3
18
21
// Explain why the output is the way it is
19
22
// =============> write your explanation here
23
+
: thearguments42,105,and806arebeingpassedintogetLastDigit(42)etc.,butsincethefunctionisdefinedasfunctiongetLastDigit()—withemptyparentheses—ithasnoparameterstoreceivethosevalues.Thepassed-inargumentsaresimplydiscarded.insidethefunctionisn't a parameter at all — it'sreferringtothecompletelyseparateouternumvariable(103)viaclosure/scope,whichiswhyeverycallgivesthesameanswerregardlessofinput.
20
24
// Finally, correct the code to fix the problem
21
25
// =============> write your new code here
26
+
functiongetLastDigit(num){
27
+
returnnum.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)}`);
22
33
23
34
// This program should tell the user the last digit of each number.
24
35
// Explain why getLastDigit is not working properly - correct the problem
0 commit comments