Meal Maker challenge. don't uderstand how a method can be access to a object property in a other method

Hello everyone! I am a new french guy here (in the forum and in the coding). Explaining my problem in english is difficult for me, I will try to be as clear as possible (I have a little level in english).

In the generateRandomMeal() method, in it last declaration return console.log().
I don’t understand how {}, {}, ${} can be refer to the dish object properties declared in the addDishToCourse Method ?

I don’t uderstand the scope of the object dish.

Thanks you for your ranswers.

let’s see my code below.

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: []
  // ajoute un plat dans le menu
  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice
    return this._courses[courseName].push(dish); 
  // renvois un plat au hasard
  getRandomDishFromCourse(courseName) {
    let dishes = this._courses[courseName];
    let randomDishes = Math.floor(Math.random() * dishes.length);
    return dishes[randomDishes]; 
  // génère un menu
  generateRandomMeal() {
   let appetizer = this.getRandomDishFromCourse('appetizers');
   let main = this.getRandomDishFromCourse('mains');
   let dessert = this.getRandomDishFromCourse('desserts');
   let totalPrice = appetizer.price + main.price + dessert.price;
   return console.log(`The appetizer is ${}, the main is ${}, the dessert is ${} and the price is ${totalPrice}.`)

menu.addDishToCourse('appetizers','salad', 9);
menu.addDishToCourse('appetizers', 'maquereau', 8.5);
menu.addDishToCourse('appetizers', 'quiche', 8);

menu.addDishToCourse('mains','pizza', 12.90);
menu.addDishToCourse('mains', 'bolognaise', 15);
menu.addDishToCourse('mains', 'pomme de terre', 8);

menu.addDishToCourse('desserts','yahourt', 9);
menu.addDishToCourse('desserts', 'glace', 13);
menu.addDishToCourse('desserts', 'cake', 8);

const meal = menu.generateRandomMeal();


addDishtoCourse pushes the object in the appropriate course (so for example mains), so then getRandomDishFromCourse gets the object from the array.

you can log the courses object:

const meal = menu.generateRandomMeal();

to see that each course has an array with objects

Hello stetim94, thanks for your reply!

Yes, I understand the process and the goals of the Methods.

That I don’t understand it is how

in the generateRandomMeal method

can be access to the object (dish) properties (pairs key-value)

in the addDishtoCourse method for return the value (‘pasta’ for exemple) name.

look at this line of the addDishToCourse method:

return this._courses[courseName].push(dish); 

you push the object (dish) into the appetizers, mains or desserts array.

which means we now always have access, so getRandomDishFromCourse will retrieve a random element/object from the array.

1 Like

Yes! :smiley:

I finaly undestand !

Thanks you very much for your answer!