Below I reproduce my code for calculate average ratings for foods.
3+4+5+2 / 4 should be equal to 3.5. But this not gives me the correct output. What's wrong with my code?
const ratings = [
{food:3},
{food:4},
{food:5},
{food:2}
];
let food = 0;
ratings.forEach((obj,index)=>{
food = (food + obj.food)/++index
})
console.log("FOOD",food)