Skip to content

Commit aeac076

Browse files
Return correct value from sum function
1 parent 2ff3562 commit aeac076

File tree

1 file changed

+13
-1
lines changed
  • Sprint-2/2-mandatory-debug

1 file changed

+13
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3-
3+
// The function returns nothing because the return statement has no value.
4+
// Once JavaScript reaches return, the function stops executing.
5+
// Therefore, the line after it is dead code and never runs.
6+
// Since the function does not return a value, it returns undefined, which is why the template string displays undefined.
47
function sum(a, b) {
58
return;
69
a + b;
@@ -9,5 +12,14 @@ function sum(a, b) {
912
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1013

1114
// =============> write your explanation here
15+
// The function now correctly returns the result of a + b.
16+
// Since the addition is included in the return statement, the function sends back the calculated value.
17+
// Therefore, when the function is called inside the template string, it displays the correct result, 42.
1218
// Finally, correct the code to fix the problem
1319
// =============> write your new code here
20+
21+
function sum(a, b) {
22+
return a + b;
23+
}
24+
25+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

0 commit comments

Comments
 (0)