Tell us what’s happening:
From the curriculum up to this point, I have no idea what I’m doing wrong. The other forum posts are using loops, but the curriculum hasn’t gone over loops yet, so I don’t know how to make this work. The math is coming out correctly, but I don’t know what I’m doing wrong.
Your code so far
const recipes = [];
const recipe1 = {
name: 'Spaghetti Carbonara',
ingredients: ['spaghetti', 'Parmesan cheese', 'pancetta', 'black pepper'],
cookingTime: 22,
totalIngredients: null,
difficultyLevel: '',
ratings: [4, 5, 4, 5],
averageRating: null,
};
const recipe2 = {
name: 'Chicken Curry',
ingredients: ['chicken breast', 'coconut milk', 'curry powder', 'onion', 'garlic'],
cookingTime: 42,
totalIngredients: null,
difficultyLevel: '',
ratings: [4, 5, 5, 5],
averageRating: null,
};
const recipe3 = {
name: 'Vegetable Stir Fry',
ingredients: ['broccoli', 'carrot', 'bell pepper'],
cookingTime: 15,
totalIngredients: null,
difficultyLevel: '',
ratings: [4, 3, 4, 5],
averageRating: null,
};
recipes.push(recipe1, recipe2, recipe3);
// User Editable Region
const getAverageRating = function(arr) {
let sum = arr.ratings[0] + arr.ratings[1] + arr.ratings[2] + arr.ratings[3];
let avg = sum/arr.ratings.length;
return avg;
}
console.log(getAverageRating(recipe1)); //4.5
console.log(getAverageRating(recipe2)); //4.75
console.log(getAverageRating(recipe3)); //4
// User Editable Region
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Challenge Information:
Build a Recipe Tracker - Step 7