|
1 | 1 | // Predict and explain first... |
| 2 | +// The console will print: |
| 3 | +// 320 |
| 4 | +// The result of multiplying 10 and 32 is undefined |
2 | 5 |
|
3 | 6 | // =============> write your prediction here |
| 7 | +// The first `console.log` inside the function prints the multiplication result (320). |
| 8 | +// However, since `multiply()` does not return anything, the outer `console.log` |
| 9 | +// receives `undefined` as the value of the function call, leading to the message |
| 10 | +// "The result of multiplying 10 and 32 is undefined". |
4 | 11 |
|
| 12 | +/** |
5 | 13 | function multiply(a, b) { |
6 | 14 | console.log(a * b); |
7 | 15 | } |
8 | 16 |
|
9 | 17 | console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); |
| 18 | +*/ |
10 | 19 |
|
11 | 20 | // =============> write your explanation here |
12 | | - |
| 21 | +// The first `console.log` inside the function prints the multiplication result (320). |
| 22 | +// However, since `multiply()` does not return anything, the outer `console.log` |
| 23 | +// receives `undefined` as the value of the function call, leading to the message |
| 24 | +// "The result of multiplying 10 and 32 is undefined". |
| 25 | +// |
| 26 | +// To fix this, the function should `return` the result instead of just logging it. |
13 | 27 | // Finally, correct the code to fix the problem |
| 28 | + |
14 | 29 | // =============> write your new code here |
| 30 | +function multiply(a, b) { |
| 31 | + return a * b; |
| 32 | +} |
| 33 | + |
| 34 | +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); |
| 35 | +// Output: "The result of multiplying 10 and 32 is 320" |
0 commit comments