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-2/2-mandatory-debug/2.js
+21Lines changed: 21 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,7 @@
2
2
3
3
// Predict the output of the following code:
4
4
// =============> Write your prediction here
5
+
//I predict the code will output the number '3' for every function call.
5
6
6
7
constnum=103;
7
8
@@ -15,10 +16,30 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15
16
16
17
// Now run the code and compare the output to your prediction
17
18
// =============> write the output here
19
+
20
+
//OUTPUT: '3' was the output for every function call, which is not what I predicted.
21
+
18
22
// Explain why the output is the way it is
19
23
// =============> 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
+
20
28
// Finally, correct the code to fix the problem
21
29
// =============> write your new code here
22
30
31
+
functiongetLastDigit(num){
32
+
returnnum.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
+
23
39
// This program should tell the user the last digit of each number.
40
+
24
41
// 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