Please help! I need to declare an empty array (foodTray = []), and run a loop 5x (for each iteration push a food type), the foodTray has to hold a max of 2 types but the total length should not exceed 5.
const foodTypes = ['seafood', 'nuts', 'seeds'];
I had done this =>
'use strict';
let foodTray = [];
const foodTypes = ['seafood', 'nuts', 'seeds'];
for (let x = 0; x < foodTypes.length; x++) {
const food = foodTypes[x];
foodTray.push(food);
if (foodTray.length < 5) {
foodTray.push(food);
}
}
console.log('I have ' + `${foodTray}.`);
but I was instructed the loop had to run 5 times***** and what I used was the length of the foodTypes.
The desired output should be => I have seafood, seafood, nuts, nuts, seeds.
The problem: I did for (let x = 0; x < *foodTypes.length*; x++)
instead, the loop should run 5 times! I am not sure how I can iterate twice over an element.
foodin yourfoodtrayand add it to yourifstatement?while (foodTray.length < 5 && foodTray.filter(f => f == food).length < 2) { foodTray.push(food)}