Build a Recipe Tracker - Step 7

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

Adding arr.ratings = avg; before my return line doesn’t help either.

your implementation doesn’t match the requirements

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

this is not how you get values from an array

same for your testing, you are not testing with an array

then how do you get values from an array? The lessons previous to this didn’t show me another way to do it.

you need to treat arr as array, not as an object that contains an array

if you call the function with an array you should see the issue

1 Like

Okay. That helped. Thank you. I’m new at this I sometimes get stuck on little things like that. Here’s my corrected code that passed.

const getAverageRating = function(arr) {
  let sum = arr[0] + arr[1] + arr[2] + arr[3];
  let avg = sum/arr.length;
  return avg;
}

After looking at it again, I can see the mistake. I think I was trying to be too specific, when this function could used to calculate any average from an array using 4 digits. I’m guessing loops let us expand that in the future. What I was specifically missing I think, was that arr will work the same as obj.arr when trying to do call them into functions. For some reason I thought I had to call the object and then call the specific array inside the function, which probably makes the function too specific to really be useful, or might be bad practice. Either way, thank you for the nudge!

they do not work the same, one is for an array, the other is for an object that contains an array
the first one expects an argument such as [1, 2, 3, 4], the other an argument such as {arr: [1, 2, 3, 4]}