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
// I predict that the error will occur because the function 'sum' does not return any value, so when we try to use it inside the template literal, it will return 'undefined'.
4
+
// This will result in the output being "The sum of 10 and 32 is undefined" instead of the expected sum of 10 and 32.
4
5
functionsum(a,b){
5
6
return;
6
7
a+b;
@@ -9,5 +10,12 @@ function sum(a, b) {
9
10
console.log(`The sum of 10 and 32 is ${sum(10,32)}`);
10
11
11
12
// =============> write your explanation here
13
+
// The error occurs because the function 'sum' has a return statement that does not return any value, which means it returns 'undefined' by default.
14
+
// When we try to use the result of 'sum(10, 32)' inside the template literal, it evaluates to 'undefined', which is not the expected output.
15
+
// To fix this error, we need to change the return statement in the 'sum' function to return the sum of 'a' and 'b'.
Copy file name to clipboardExpand all lines: Sprint-2/2-mandatory-debug/2.js
+16-1Lines changed: 16 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
// Predict the output of the following code:
4
4
// =============> Write your prediction here
5
-
5
+
// I predict that the output will be "The last digit of 42 is 3", "The last digit of 105 is 3", and "The last digit of 806 is 3" because the function 'getLastDigit' is using the variable 'num' which is set to 103, so it will always return the last digit of 103, which is 3, regardless of the input passed to the function.
6
6
constnum=103;
7
7
8
8
functiongetLastDigit(){
@@ -15,10 +15,25 @@ 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
+
// The output is "The last digit of 42 is 3", "The last digit of 105 is 3", and "The last digit of 806 is 3". This matches my prediction because the function 'getLastDigit' is using the variable 'num' which is set to 103, so it will always return the last digit of 103, which is 3, regardless of the input passed to the function.
18
19
// Explain why the output is the way it is
19
20
// =============> write your explanation here
21
+
// The output is the way it is because the function 'getLastDigit' is not using the parameter 'number' that is passed to it.
22
+
// Instead, it is using the variable 'num' which is set to 103.
23
+
// Since 'num' is not changing based on the input, the function will always return the last digit of 103, which is 3, regardless of the input passed to the function. To fix this issue, we need to change the function to use the parameter 'number' instead of the variable 'num'.
20
24
// Finally, correct the code to fix the problem
21
25
// =============> write your new code here
26
+
constnum=103;
27
+
28
+
functiongetLastDigit(number){
29
+
returnnumber.toString().slice(-1);
30
+
}
31
+
32
+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
33
+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
34
+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
22
35
23
36
// This program should tell the user the last digit of each number.
24
37
// Explain why getLastDigit is not working properly - correct the problem
38
+
// GetLastDigit seems not to work properly because the variable num is never passed into the function.
39
+
// The fix is to call getLastDigit(num) so the function uses that value.
0 commit comments