Skip to content

Commit 1743311

Browse files
committed
Add correct ingredient logging by looping through recipe.ingredients
1 parent c76ea74 commit 1743311

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

‎Sprint-2/debug/recipe.js‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Each ingredient should be logged on a new line
55
// How can you fix it?
66

7-
const recipe = {
7+
/*const recipe = {
88
title: "bruschetta",
99
serves: 2,
1010
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
@@ -13,3 +13,26 @@ const recipe = {
1313
console.log(`${recipe.title} serves ${recipe.serves}
1414
ingredients:
1515
${recipe}`);
16+
*/
17+
18+
// Prediction and Explanation
19+
/*
20+
The line inside the template string: ${recipe} does not print the ingredients.
21+
Instead, JavaScript converts the entire recipe object into a string, which becomes "[object Object]". This happens
22+
because plain objects {} are not automatically converted into readable text. The program should print each ingredient on a new line, but the current code
23+
never loops through recipe.ingredients, so nothing is printed correctly.
24+
*/
25+
26+
// Corrected code
27+
const recipe = {
28+
title: "bruschetta",
29+
serves: 2,
30+
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
31+
};
32+
33+
console.log(`${recipe.title} serves ${recipe.serves}
34+
ingredients:`);
35+
36+
for (const ingredient of recipe.ingredients) {
37+
console.log(ingredient);
38+
}

0 commit comments

Comments
 (0)