Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created June 16, 2021 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/970b5b75d11de5c154762f1c4ebd4d88 to your computer and use it in GitHub Desktop.
Save codecademydev/970b5b75d11de5c154762f1c4ebd4d88 to your computer and use it in GitHub Desktop.
Codecademy export
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 appetizers(appetizers) {
this._courses.appetizers = appetizers;
},
set mains(mains) {
this._courses.mains = mains;
},
set desserts(desserts) {
this._courses.desserts = 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._courses[courseName].push(dish);
},
getRandomDishFromCourse(courseName) {
const dishes = this._courses[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}, ${dessert.name}. The price is ${totalPrice}.`
}
};
menu.addDishToCourse('appetizers', 'velvet cake', 50);
menu.addDishToCourse('appetizers', 'chocolate cake', 50);
menu.addDishToCourse('appetizers', 'vanilla cake', 50);
menu.addDishToCourse('mains', 'Rice and Chicken', 100);
menu.addDishToCourse('mains', 'Rice and Beans', 100);
menu.addDishToCourse('mains', 'Amala and Abula', 100);
menu.addDishToCourse('desserts', 'ice cream', 50);
menu.addDishToCourse('desserts', 'salad', 50);
menu.addDishToCourse('desserts', 'soda', 50);
const meal = menu.generateRandomMeal();
console.log(meal);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment