Build a Recipe Tracker - Step 7

Tell us what’s happening:

the purpose is to get the average rating. What seems to be the issue? Please help. Thanks!

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

function getAverageRating(ratings) {
  total = 0
  for (let elem of recipes) {
    for (let item of elem.ratings) {
      total += item
    }
  }
  return total / 4
}

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0

Challenge Information:

Build a Recipe Tracker - Step 7

Welcome to the forum @abhatia08

The code looks good. Just some minor tweaks.

  • declare total with keyword let - let total = 0
  • Note that the function has parameter “ratings”. So the for loop should run on “ratings” and not recipes.
  • That means we need only one loop. If you do a console.log(elem) you will be able to fix it.

Happy Coding!

1 Like