Skip to content

Commit bdc5ac9

Browse files
committed
Correct return statement in sum function to return a + b
1 parent 2f9d4da commit bdc5ac9

File tree

1 file changed

+16
-2
lines changed
  • Sprint-2/2-mandatory-debug

1 file changed

+16
-2
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
32

3+
// =============> write your prediction here
4+
// I predict that this will print: "The sum of 10 and 32 is undefined"
5+
// because the function has a return statement with no value.
6+
7+
// Explanation:
48
function sum(a, b) {
59
return;
610
a + b;
11+
// =============> write your explanation here
12+
// The `return;` statement ends the function early, so `a + b` is never reached.
13+
// In JavaScript, any code after a bare `return;` is ignored (unreachable code).
14+
// That's why the function returns `undefined`, not the actual sum.
715
}
816

917
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1018

11-
// =============> write your explanation here
1219
// Finally, correct the code to fix the problem
1320
// =============> write your new code here
21+
function sum(a, b) {
22+
return a + b;
23+
}
24+
25+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
26+
// Now the function returns the correct result: 42
27+

0 commit comments

Comments
 (0)