Skip to content

Commit 0210aba

Browse files
- Fix multiply function to return result instead of logging it
- Pass returned value into template literal expression
1 parent 307d6b9 commit 0210aba

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

  • Sprint-3/2-mandatory-debug

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22

33
// =============> write your prediction here
44

5-
function multiply(a, b) {
6-
console.log(a * b);
7-
}
5+
// =============> Prediction: The function will log 320 to the terminal when called, but the template string will evaluate to "The result of multiplying 10 and 32 is undefined" because the function does not return a value but rather 'console.log(a * b)'.
6+
7+
// function multiply(a, b) {
8+
// console.log(a * b);
9+
// }
10+
11+
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
812

9-
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
13+
// =============> write your explanation here
1014

11-
// =============> write your explanation here
15+
// =============> Explanation: When I logged the code in my console, I got this message(The result of multiplying 10 and 32 is undefined). This is because, in JavaScript, every function returns 'undefined' by default unless a 'return' keyword is directly used. Calling console.log() inside a function only outputs to the console without passing a value back. Replacing console.log() with return inside the function body allows the calculated result (320) to be passed directly into the template string.
1216

1317
// Finally, correct the code to fix the problem
1418
// =============> write your new code here
19+
function multiply(a, b) {
20+
return a * b;
21+
}
22+
23+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // output 320

0 commit comments

Comments
 (0)