Meal Maker project - Total price (point 11)

Hello! I have a pretty intricate question. Let’s see if I can explain myself.

In point 11 of the mention project, I do not get fully understand why I can write “appetizer.price + main.price + dessert.price” in order to get the total. If I am not mistaken, I can do that because I refer to the appetizer, main, and dessert belonging to the arrays. But shouldn’t I somehow use the ‘this’ keywork?
Also -and this is partially related to the former doubt-, if I push with addDishToCourse two elements (a dish and a price) to the corresponding array, how can the function getRandomDishFromCourse pick a random dish? If the index picked is random, it may happen it to pick a price, and not necessarily a dish…I’m a bit confused.

I really hope I explained myself pretty well. By the way, it’s an exercise very difficult to fully get…Thank you very much for the help!

I assume the code looks something like this:

appetizer = getRandomDishFromCourse('appetizers');

given you posted code nor exercise url

so appetizer variable now holds an object with two properties: name and price

to get a property of an object we can do:

obj.prop

in general syntax, so in this specific case:

appetizer.price

because within addDishToCourse , you construct an object with the properties. This object is then placed within the array

so when we retrieve a random element from the array, we get an object with the information belonging to a single dish (name and price)

Sorry, indeed I did’t paste you the code.

"const menu = {
_courses : {
appetizers: ,
mains: ,
desserts: ,

get appetizers() {
return this._courses.appetizers;
},

get mains() {
return this._courses.mains;
},

get desserts() {
return this._courses.desserts;
},

set desserts(appetizers) {
this._courses.appetizers = appetizers
},

set desserts(mains) {
this._courses.mains = mains
},

set desserts(desserts) {
this._coursesdesserts = desserts
},

},

get courses() {
return {
appetizers : this.appetizers,
mains : this.mains,
desserts : this.desserts,
}
},

addDishToCourse(courseName, dishName, dishPrice) {
const dish = {
name : dishName,
price: dishPrice,
};
return this._course[courseName].push(dish)
},

getRandomDishFromCourse(courseName) {
const dishes = this.course[courseName];
const randomIndex = Math.floor(Math.random() * dishes.length);
return dishes[randomIndex];
},

generateRandomMeal() {
const appetizer = this.getRandomDishFromCourse(‘appetizers’);
const main = this.getRandomDishFromCourse(‘mains’);
const dessert = this.getRandomDishFromCourse(‘desserts’);
const TotalPrice = appetizer.price + main.price + dessert.price;

return Your meal is ${appetizer.name}, ${main.name}, and ${dessert.name}, and the total price is ${totalPrice}
}

};"

WIth regards to the second point you explained, it should be clear. Basically, please correct me if I’m not right, when we push an object in an array, then both pieces of information belonging to the object (in this case, name and price) are assumed to be stored in the same index. If this is true, then I understood!

The first part of my question troubles me a bit more, even if it looks a bit easier. ‘generateRandomMeal’ is a function, and as such inside of it we do not have access to the other object’s properties, and thus I do not get why we can use the canonical ‘obj.key’ to access pieces of data that are stored in the array.

I thank you very much in advance for the help!

yes, exactly :slight_smile: You can always use console.log() to log additional information to the console, this can be helpful to “see” what your code is doing

there also debug tools to help you with this:

its not. generateRandomMeal is a method of/belonging to menuobject.

which means we do have access:

const myObj = {
  a: 3,
  b() {
    console.log(this.a)
  }
}

myObj.b()

within an object, we can access properties and methods of that object through this