lundi 14 décembre 2020

can I use forEach loop along with the conditional statement if to only print out only even elements?

I'm at the very very very beginning of JS learning curve.
I am trying to use a loop on an array (containing six types of food) and then only print out the even elements:

const foodArray = ["pizza", "tacos", "fruit", "veggies", "hummus", "tea"];

foodArray.forEach(food => {
  if (foodArray.length % 2 = 0) {
  console.log(`${food}`); 
  }
});

Is this totally wrong? Should I use either for or while loop in association with the if conditional statement?

Aucun commentaire:

Enregistrer un commentaire