9.10.1 Studio: Loops

I was able to assemble the 6 meals, but my code was lengthy, and I feel like I must have been missing a shortcut. I copy and pasted to code six times and edited each array name and each index. Can anyone tell me how I could have done this more efficiently? Here was what my code looked like.

for (i = 0; i < 1; i++){
meal1.push(pantry[0][0]), meal1.push(pantry[1][0]), meal1.push(pantry[2][0]), meal1.push(pantry[3][0]), meal1.push(pantry[4][0]);
console.log(Winner winner chicken dinner: ${meal1})
}

Several other students & myself didn’t save the meals individually. We looped through the pantry array to display them as individual meals but that’s it. The rest of the studio can still be completed (user query + kale-related message) this way.

1 Like

Thank you! Could you give me an example of the code you used, or tell me what array method you used? I tried:

pantry.push(protein[ i ]), pantry.push(vegetable[ i ]), ect…

but it isn’t giving me the desired results. I can’t figure out what the code needs to look like.

for (let i = 0; i <= userChoice - 1; i++){
console.log(Meal Option ${i + 1});
for (let j = 0; j < pantry.length; j++){
console.log(Meal contains: ${pantry[j][i]});
}
}

My code is already refactored to take user input into account. Warning = it isn’t the best display, I intended to get back in it to clean it up but got busy with other things…maybe soon!