-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.js
More file actions
46 lines (35 loc) · 1.66 KB
/
Copy path1.js
File metadata and controls
46 lines (35 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Predict and explain first...
// =============> write your prediction here
// Answer
// I predict that calling sum(10, 32) will return `undefined`.
// This is because the `return` statement has a semicolon immediately after it,
// so JavaScript interprets it as `return;` which evaluate to "undefined" when the function is called
// The expression `a + b` on the next line will never be executed.
// Therefore, any console.log using sum(10, 32) will display `undefined` in the output.
//function sum(a, b) {
//return;
//a + b;
//}
//console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
// =============> write your explanation here
// Explanation
// function sum(a, b) {
// This declares a function named sum that takes two parameters: a and b.
// return;
// The return statement is immediately followed by a semicolon.
// This causes the function to exit immediately.
// Since nothing is returned explicitly, JavaScript returns undefined by default.
// a + b;
// This line never executes because the function has already exited at the `return;` line.
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
// In this expression, sum(10, 32) is called.
// Because the function returns undefined, the template literal becomes: "The sum of 10 and 32 is undefined"
// That string is printed to the console.
// Finally, correct the code to fix the problem
// I will fix the error by placing the expression a + b on the same line as the return statement.
// Optionally, I can use parentheses around the expression, but this is not required.
// =============> write your new code here
function sum(a, b) {
return (a + b);
}
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);