Price is cumulative because each part of the meal cost something. In this you have a main course with a dessert and appetizer, these all cost different things. I am adding all of these together so that when I log the final meal I have the total price instead of the price of just one item that they ordered. Also, thank you for telling me about the “this” thing.
My full code is:
var menu = {
_courses: {
appetizers: [],
mains: [],
desserts: []
},
get appetizers() {
return this._courses.appetizers;
},
get mains() {
return this._courses.mains;
},
get desserts() {
return this._courses.desserts;
},
set appetizers(appetizers) {
this.courses.desserts = desserts;
},
set mains(mains) {
this.mains = mains;
},
set desserts(desserts) {
this.desserts = desserts;
},
get courses() {
return {
appetizers,
mains,
desserts,
};
},
addDishToCourse (courseName, dishName, dishPrice) {
const dish = {
food: courseName,
name: dishName,
price: [],
totalPrice: 0,
};
for (var i = 0; i < 8; i++) {
this.totalPrice = this.totalPrice + this.price[i];
}
},
getRandomDishFromCourse: function(courseName) {
var dishes = this._courses[courseName];
if (dishes !== undefined) {
var randomIndex = Math.floor(Math.random() * dishes.length);
return randomIndex;
}
},
generateRandomMeal: function() { //This is the problem.
var appetizer = this.getRandomDishFromCourse('appetizers');
var main = this.getRandomDishFromCourse('mains');
var dessert = this.getRandomDishFromCourse('dessert');
return 'Your meal is ' + this.appetizers[appetizer].name + ', ' + this.mains[main].name + ', and ' + this.desserts[dessert] + '. The total price is $' + this.totalPrice + '.';
}
};
menu.addDishToCourse('appetizers', 'Bread Loaf', 4.25);
menu.addDishToCourse('appetizers', 'Caesar Salad', 3.50);
menu.addDishToCourse('appetizers', 'Small Soup', 4.50);
menu.addDishToCourse('mains', 'Steak Dinner', 20.00);
menu.addDishToCourse('mains', 'Spaghetti', 16.00);
menu.addDishToCourse('mains', 'Turkey Dinner', 18.99);
menu.addDishToCourse('desserts', 'Icecream', 5.99);
menu.addDishToCourse('desserts', 'Cookies', 5.99);
menu.addDishToCourse('desserts', 'Jello', 5.99);
var meal = menu.generateRandomMeal();
console.log(meal);