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
+21-4Lines changed: 21 additions & 4 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:I predict the output will be 3 every time, because the function uses the global constant num = 103 instead of using the numbers passed into getLastDigit().
5
5
6
6
constnum=103;
7
7
@@ -14,11 +14,28 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14
14
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
-
// =============> write the output here
17
+
// =============> 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
18
21
// Explain why the output is the way it is
19
-
// =============> write your explanation here
22
+
// =============> write your explanation here:there is no parameter in the function.
23
+
//So when we call the function:
24
+
//getLastDigit(42)
25
+
//the 42 is passed to the function, but the function has no parameter to receive it.
26
+
//Instead, it uses the global variable (const num = 103)
27
+
//so every call converts 103 to a string and gets its last digit: 3.
20
28
// Finally, correct the code to fix the problem
21
-
// =============> write your new code here
29
+
// =============> write your new code here:
30
+
constnum=103;
31
+
32
+
functiongetLastDigit(num){
33
+
returnnum.toString().slice(-1);
34
+
}
35
+
36
+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
37
+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
38
+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
22
39
23
40
// This program should tell the user the last digit of each number.
24
41
// Explain why getLastDigit is not working properly - correct the problem
0 commit comments