Chapter 9.10 Studio: Loops (Part A)

I completed the LC101 program in 2018. I’m working through the JavaScript course materials to learn JavaScript and front-end web development. I’m having trouble with Studio 9.10 Part A.

Use a for loop to assemble 6 meals.

  1. The meals must include one item from each of the source arrays.
  2. Each ingredient can only be used ONCE.
  3. Print out each meal.

I created six empty arrays for each meal. Then, I initialized an empty array to hold all of the meal arrays. I tried to use two for loops to add items to more than one meal. In one instance, it just repeated the user’s item about four times per meal.

I was able to solve it for creating one meal, including user input, but not for multiple meals.

I also was able to complete Assignment 1. I’ll try to apply the same approach to this problem.

If anyone could help me understand the instructions better, I would appreciate it.

Try just one loop:
for (let i = 0; i < 6; i++)

Make one empty array for each iteration:
mealArray[i]

Then push each menu Array[i] into mealArray[i] – You can do this all at once by something like this:
mealArray[i].push(proteinArray[i], grainArray[i], vegetableArray[i],
beverageArray[i], dessertArray[i])
– Note the parentheses, so you are saying for each iteration of the loop[i] create a mealArray[i] then push into it one item[i] from each Array.

After that, just print the mealArray :
console.log(mealArray[i]

Finally, don’t forget to iterate your loop
i++;

so the whole snippet is something like this:

//while(i < howMany){ (Uncomment this line and Comment out the for line below to use with user input)
for (let i = 0; i < 6; i++){
mealArray[i].push(proteinArray[i], grainArray[i], vegetableArray[i],
beverageArray[i], dessertArray[i])
console.log(mealArray[i])
i++;
}

Hope that helps!

1 Like

Thank you for your feedback! It helped me look at the problem in a different way.

@dconford With user input, are users required to select one item per pantry category? Or would users only input how many meals they want the program to create, preventing individual item selection? If it’s the former, I may be stuck again. I have set for the meals to be built by iterating through each index of the pantry array and incrementing index01 by 1 at the end of each loop.

// this would loop until each category has been covered
meal.push(pantry[index01][index02], pantry[index01 + 1][index02], ...);
index01++;

No, it’s just supposed to loop through in order. You will get the same meals each time.
User can select 1- 6 meals but will get same meal 1 , meal2 every time.
Don’t worry, it gets more complex pretty quickly.

1 Like

Hi,

I’m assuming you are talking about “9.10.4. Part C (Refinements)”. As per the requirement: ask the user to select the number of meals to assemble.

So, “users only input how many meals they want the program to create”. You do not need to implement an ability by the user to customize a meal (no “Build Your Own” feature). You are definitely overthinking the meal creation exercises. Other than the optional “array of arrays”/pantry exercise, it can all be accomplished by a single for loop iterating over 1-dimensional arrays.

1 Like

Thanks! I really did overthink Part A and Part C of the studio. My current code is able to make the same meals, depending on how many meals the user selects. Thank you for your help!

Yeah, I was referring to Part C with my last question. I definitely did overthink the meal creation studio lol. I was able to implement the program to print the fixed meals based on the number of meals the user entered. Thank you for your help!