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?
for (let i = 1; i < foodArray.length; i += 2) { console.log(foodArray[i]) }) so that you avoid the first unnecessary loop and you can use directlyfoodArray[i]instead offoodArray[i+1]