Build a Recipe Tracker - Step 7

Cuéntanos qué está pasando:

Step 7:
Now, you should work on calculating the averageRating, totalIngredients, and the difficultyLevel for each recipe in the recipes array.

Start by creating a getAverageRating function that takes a single argument, which is an array with ratings. Inside the function, calculate the average rating from the array passed to the function.

Your getAverageRating function must return a number.

What is the problem?

The Hint is say: Your getAverageRating function should return a number.

Tu código hasta el momento

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 (arr) {
  let result = 0;
  for(let elem of arr) {
    let total = 0;
    for(let item of elem.ratings){
      total += item;
    }
    total /= 4;
    result += total;
  }
  return result / 3;
}
console.log(getAverageRating(recipes));


// User Editable Region

Información de tu navegador:

El agente de usuario es: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36

Información del Desafío:

Build a Recipe Tracker - Step 7

getAverageRating function that takes a single argument, which is an array with ratings

An array of ratings from a single recipe will be passed to the function.