11// Predict and explain first...
22
33// =============> write your prediction here
4+ // Prediction: The function will not multiply the numbers.
5+ // Instead, it will print the sum of a and b (42) and the final console.log will ouput:
6+ // The result of multiplying 10 and 32 is undefined
47
8+ /* Original code
59function multiply(a, b) {
610 console.log(a * b);
711}
812
913console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
14+ */
15+
16+ // =============> Error message
17+ // 320
18+ // The result of multiplying 10 and 32 is undefined
1019
1120// =============> write your explanation here
21+ // MDN Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return
22+
23+ // The function multiply() uses console.log() to print the result but it does not return anything.
24+ // In JavaScript, if a function does not explicitly return a value, it returns undefined.
25+ // When we try to use multiplay(10, 32) inside a console.log, it returns undefined.
26+ // To fix this, we must use the return statement so the function gives back a value that can be used.
1227
1328// Finally, correct the code to fix the problem
1429// =============> write your new code here
30+ function multiply ( a , b ) {
31+ return a * b ;
32+ }
33+
34+ // The result of multiplying 10 and 32 is 320
35+ console . log ( `The result of multiplying 10 and 32 is ` + multiply ( 10 , 32 ) ) ;
0 commit comments