Build a Recipe Tracker - Step 7

Tell us what’s happening:

I have error in step 7 “Your getAverageRating” function should return a number. Please tell me, what could be the problem?

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 (recipearr) {
  const ratingsValue = recipearr.ratings;
  //console.log(ratingsValue);
  
  return ratingsValue.reduce((sum, current) => sum + current, 0) / ratingsValue.length;
}

// 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/138.0.0.0 Safari/537.36

Challenge Information:

Build a Recipe Tracker - Step 7
https://www.freecodecamp.org/learn/full-stack-developer/workshop-recipe-tracker/step-7

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

read again this sentece, and double check what you have written

I am not a native English speaker, so I believe that I may be mistaken in the correct understanding of the task. Sorry.

the thing you need to understand here is what is required as argument

a single argument, which is an array with ratings

and what your function is instead doing

this is not written for the argument to be an array of ratings

1 Like

[4, 5, 4, 5]

This is from the recipe1 object’s rating property and is an example of what would be passed as a parameter to getAverageRating().

1 Like

Thank you. I understood. I’ve complicated things unnecessarily.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.