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!

2 Likes

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!

This will show the menu for each astronaut:

for (const a in PROTEIN && GRAIN && VEGETABLE && BEVERAGE && DESSERT) {
console.log(${PROTEIN[a]} ${GRAIN[a]} ${VEGETABLE[a]} ${BEVERAGE[a]} ${DESSERT[a]});
}

I think it works too for assignment. For indexing we can add ${a} to console.

I am currently in LC101 Independent Course. I am struggling with this problem in Chapter 9 Studio. I continue to get a
"TypeError: Cannot read property ‘push’ of undefined’

When I try to run my code following the examples in this post. I’ve created an empty ‘mealArray’ variable. I have altered several different parts of my program and I get close to having the output I want but I cannot seem to get it right. Any suggestions?

Did you declare the variable that holds your array with let?

I have. It will still return the TypeError message.

Above this I have the 5 single arrays listed. Then:
let mealArray = []

In my loop I followed the example from earlier posts:

for (let i = 0; i < 6; i++){
mealArray = mealArray[i].push(proteinArray[i], grainArray[i], vegetableArray[i],
beverageArray[i], dessertArray[i])
console.log(mealArray[i]);
i++;
}

I have been able to modify this by removing the [i] from mealArray.push to get something to print out but it is not the output I want.

I got it figured out. Thanks

1 Like

Great! Since push() adds to the end of an array by its very nature(and unshift() adds to the beginning of the array), the compiler probably wasn’t able to parse the mealArray[i].push(). To insert an item at a specific index of an array, you can use splice().

I’m in the independent course and I’m also having troubles with this one. For some reason the array I am creating isn’t recognizing the grain array nor the drinks array. I tried doing what was said above, but I don’t think it’s giving me the correct answer. Is there something I’m not seeing or forgot to do?

let protein = [‘chicken’, ‘pork’, ‘tofu’, ‘beef’, ‘fish’, ‘beans’];

let grain = [‘rice’, ‘pasta’, ‘corn’, ‘potato’, ‘quinoa’, ‘crackers’];

let vegi = [‘peas’, ‘green beans’, ‘kale’,‘edamame’, ‘broccoli’, ‘asparagus’];

let drinks = [‘juice’, ‘milk’, ‘water’, ‘soy milk’, ‘soda’, ‘tea’];

let desert = [‘apple’, ‘banana’, ‘more kale’,‘ice cream’, ‘chocolate’, ‘kiwi’];

let mealArr = [];

for (let i = 0; i < 6; i++) {

mealArr.push(protein[i], grain[i], vegi[i], drinks[i], desert[i]);

console.log(mealArr[i]);

i++;

}

Yes. To create a 2D array from start, you push brackets into mealArr by the syntax, mealArr.push([]), and then you create a nested for loop that hits the bounding limit to 5 because there are 5 sections of food. Inside the nested loop, you check conditions of j variable and then push sections of food in each subarray. For outputting each meal, you output the variable, mealArr[i] after the nested for loop and before the end of the main for loop. If you want to take a look on how the algorithm is structured, see below.

const mealArr = [];

for(let i = 0; i < 6; i++) {
mealArr.push([]);

for(let j = 0; j < 5; j++) {
if(j === 0) {
mealArr[i].push(protein[i]);
}
else if(j === 1) {
mealArr[i].push(grain[i]);
}
else if(j === 2) {
mealArr[i].push(vegetables[i]);
}
else if(j === 3) {
mealArr[i].push(drinks[i]);
}
else {
mealArr[i].push(dessert[i]);
}
}

console.log(mealArr[i]);
}

Hope that helps.

1 Like