Skip to content

Commit 11c30c6

Browse files
committed
Fixed empty return in sum() and added explanation with MDN reference
1 parent 7a826d5 commit 11c30c6

File tree

1 file changed

+19
-0
lines changed
  • Sprint-2/2-mandatory-debug

1 file changed

+19
-0
lines changed

Sprint-2/2-mandatory-debug/1.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// Prediction: The code should output - "The sum of 10 and 32 is 42"
4+
// But the code will return undefined instead of 42.
35

6+
/* Original code
47
function sum(a, b) {
58
return;
69
a + b;
710
}
811
912
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
13+
*/
14+
15+
// =============> Error message
16+
// The sum of 10 and 32 is undefined
1017

1118
// =============> write your explanation here
19+
// MDN Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return
20+
21+
// The function uses `return;` without an expression.
22+
// According to MDN, if a return statement has no value, the function returns undefined.
23+
// So when `sum(10, 32)` is called, it returns undefined.
24+
// To fix this, the return statement must include the expression `a + b`.
25+
1226
// Finally, correct the code to fix the problem
1327
// =============> write your new code here
28+
function sum (a, b) {
29+
return a + b;
30+
}
31+
32+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

0 commit comments

Comments
 (0)