Skip to content

Commit b800220

Browse files
- Fix sum function to return correct value and update predictions and explanations
- Move expression a + b onto the same line as return keyword - Prevent the function from returning undefined on a newline
1 parent 0210aba commit b800220

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

  • Sprint-3/2-mandatory-debug

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
// Predict and explain first...
22
// =============> write your prediction here
33

4-
function sum(a, b) {
5-
return;
6-
a + b;
7-
}
4+
// =============> Prediction: Calling sum(10, 32) will return 'undefined', so the console will output "The sum of 10 and 32 is undefined". This is because 'return;' terminates the statement immediately, leaving 'a + b;' as unreachable code.
5+
6+
// =============> Prediction: The function will evaluate to 'The sum of 10 and 32 is undefined' because there is a semi-colon at the end of the return which ends the statement immediately, leaving 'a + b' with no way of reaching the return statement.
87

9-
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
8+
// function sum(a, b) {
9+
// return;
10+
// a + b;
11+
// }
12+
13+
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1014

1115
// =============> write your explanation here
16+
17+
// =============> Explanation: When I run 'console.log(`The sum of 10 and 32 is ${sum(10, 32)}`)', I got this in my terminal 'The sum of 10 and 32 is undefined' because in JavaScript having a semi-colon at the end of a value tells the computer that this statement has ended, treating a + b; as a separate, un-returned statement. To fix this, the expression 'a + b' must sit on the exact same line as 'return'.
18+
1219
// Finally, correct the code to fix the problem
1320
// =============> write your new code here
21+
22+
function sum(a, b) {
23+
return a + b;
24+
}
25+
26+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // output The sum of 10 and 32 is 42

0 commit comments

Comments
 (0)