Meal maker

Hello everyone!
This is my first post on codecademy forums, i hope i am not violating community guidelines.
I have a doubt regarding meal maker project.Here is the link :

I have completed the project by reading on forums and seeing the project walkthrough video but i have a doubt regarding this piece of code:

generateRandomMeal () {
    let appetizer = this.getRandomDishFromCourse('appetizers');
    let main = this.getRandomDishFromCourse('mains');
    let dessert = this.getRandomDishFromCourse('desserts');

    const totalPrice = appetizer.price + main.price + dessert.price;
  return `Your meal is ${}, ${} and ${} and the total price is ${totalPrice}.`;

In generateRandomMeal() method,how can the variables appetizer,main and dessert access the price and name property of other method in menu object?
help please!

Well because Javascript works with references.

The variable appetizer in generateRandomMeal refers to whatever getRandomDishFromCourse('appetizers') returns.

getRandomDishFromCourse('appetizers') returns any meal from your list of appetizers under _courses.

A meal is defined as a data object with two properties as stated in addDishToCourse

{  name: dishName, price: dishPrice  };

So to cut this reference link short:

The variable appetizer in generateRandomMeal has a value of { name: dishName, price: dishPrice };

So to access its price property you would have to call appetizer.price

Hope this takes away your confusion.

Happy coding!


I still have a confusion on this line :
The variable appetizer in generateRandomMeal has a value of { name: dishName, price: dishPrice };

Shouldn’t we have to use ‘this’ keyword while calling appetizer.price??

No, because that reference is already made in getRandomDishFromCourse and passed on.


const dishes = this._courses[courseName]; // here
const randomIndex = Math.floor(Math.random() * dishes.length);
return dishes[randomIndex]; // and since an index value of dishes is returned it has the this reference "this"