|
1 | 1 | // Predict and explain first... |
2 | 2 |
|
3 | 3 | // =============> write your prediction here |
| 4 | +// The function multiplies the numbers and prints the answer. |
| 5 | + |
| 6 | +// But it does not return the answer. |
| 7 | + |
| 8 | +// Because there is no return, the function automatically gives back undefined. |
| 9 | + |
| 10 | +// So when we use the function inside the second console.log, the value is undefined. |
4 | 11 |
|
5 | 12 | function multiply(a, b) { |
6 | 13 | console.log(a * b); |
| 14 | + |
7 | 15 | } |
8 | 16 |
|
9 | 17 | console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); |
10 | 18 |
|
11 | 19 | // =============> write your explanation here |
| 20 | +// console.log and return are not the same. |
| 21 | + |
| 22 | +// console.log only prints the value to the console. |
| 23 | + |
| 24 | +// It does not send the value back to the function call. |
| 25 | + |
| 26 | +// Because there was no return statement, the function automatically returned undefined. |
12 | 27 |
|
13 | | -// Finally, correct the code to fix the problem |
| 28 | +// To fix the issue, I replaced console.log with return. |
| 29 | + |
| 30 | +// By returning a * b, the function now sends the calculated value back to where it was called. |
| 31 | + |
| 32 | +// As a result, the correct value is displayed instead of undefined. |
14 | 33 | // =============> write your new code here |
| 34 | +function multiply(a, b) { |
| 35 | + return (a * b); |
| 36 | + |
| 37 | +} |
| 38 | + |
| 39 | +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); |
0 commit comments