Skip to content

Commit fdf492e

Browse files
fix: correct sum function to return the sum of two numbers and update comments for clarity
1 parent dde5917 commit fdf492e

File tree

1 file changed

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

1 file changed

+8
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// I expect the code to log the sum of 10 and 32, but it will return undefined because the function "sum" does not have a return statement for the sum operation as line 7 has unreachable code due to the return statement on line 6 is returning nothing.
34

45
function sum(a, b) {
56
return;
@@ -9,5 +10,12 @@ function sum(a, b) {
910
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1011

1112
// =============> write your explanation here
13+
// The code defines a function "sum" that is intended to return the sum of two numbers "a" and "b". However, the return statement is placed before the addition operation, which means that the function will return `undefined` immediately without performing the addition. As a result, when we log the output, it will show "The sum of 10 and 32 is undefined".
1214
// Finally, correct the code to fix the problem
1315
// =============> write your new code here
16+
// To fix this we need to moving the unreachable code after the return statement into the return statement itself, so that the function returns the sum of "a" and "b".
17+
18+
function sum(a, b){
19+
return a + b;
20+
}
21+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // This will now correctly log "The sum of 10 and 32 is 42".

0 commit comments

Comments
 (0)