Build a Recipe Tracker - Step 11

Tell us what’s happening:

Hi,
Can you show me any similar example?

  1. You should access the averageRating property of recipe1.
  2. You should assign the result of calling getAverageRating with recipe1.ratings to the averageRating property of recipe1.

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);

function getAverageRating(ratings) {
  const total = ratings[0] + ratings[1] + ratings[2] + ratings[3];
  return total / ratings.length;
}

function getTotalIngredients(ingredients) {
  return ingredients.length;
}

function getDifficultyLevel(cookingTime) {
  if (cookingTime <= 30) {
    return 'easy';
  } else if (cookingTime <= 60) {
    return 'medium';
  } else {
    return 'hard';
  }
}

const recipe1AverageRating = getAverageRating(recipe1.ratings);
console.log(recipe1AverageRating);

const recipe1TotalIngredients = getTotalIngredients(recipe1.ingredients);
console.log(recipe1TotalIngredients);

const recipe1DifficultyLevel = getDifficultyLevel(recipe1.cookingTime);

// User Editable Region

console.log(recipe1DifficultyLevel);




// 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 11
https://www.freecodecamp.org/learn/full-stack-developer/workshop-recipe-tracker/step-11

you may want to review the lecture on how to access properties of objects https://www.freecodecamp.org/learn/full-stack-developer/lecture-working-with-objects/what-is-an-object-in-javascript-and-how-can-you-access-properties-from-an-object

hi,
I couldn’t solve the problem, can you help in another way, knowing that I tried many ways.
Regards,

you have an object, recipe1 and a property, averageRating
you can look back at the lecture as much as you need

show me how you access the property of that object

do you have confusions from the lecture? please ask your doubts

The lecture is clear but I couldn’t answer question 11.

ok, let’s work on it

so if you have an object named family, and you want to access the property cousin, how would you do it?

there two ways :1- family.cousin
2-family[“cousin”]

great! now do it again with an object, recipe1 and a property, averageRating

recipe1.averageRating

great
the next thing you need to do is to call a function and pass it an argument
can you show me how you call getAverageRating with an argument of recipe1.ratings?

getAverageRating (recipe1.ratings) 

great, now the next thing is assignment
do you know how to assign a value to something?

if you want you can show directly your attempt for

You should assign the result of calling getAverageRating with recipe1.ratings to the averageRating property of recipe1.

const recipe1averageRatingproperty = getAverageRating(recipe1.ratings);

what is this? this is not the syntax you showed me earlier

do you need to create a variable now?

Thank you very much , I got it .
removed by moderator

Glad you got it, please do not add solution code to the forum

1 Like