Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created August 18, 2020 15:49
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/d664c7646f13c7fac15872b42c0c8f23 to your computer and use it in GitHub Desktop.
Save codecademydev/d664c7646f13c7fac15872b42c0c8f23 to your computer and use it in GitHub Desktop.
Codecademy export
const menu = {
_courses: {
appetizers: [],
mains: [],
desserts: [],
},
get appetizers(){
return this._courses.appetizers;
},
set appetizers(meal){
if (typeof meal === 'object'){ //checks that the input is of the correct data type
this._courses.appetizers.push(meal);
} else {
console.log('Please input meal as object');
}
},
get mains(){
return this._courses.mains;
},
set mains(meal){
if (typeof meal === 'object'){ //checks that the input is of the correct data type
this._courses.mains.push(meal);
} else {
console.log('Please input meal as object');
}
},
get desserts(){
return this._courses.desserts;
},
set desserts(meal){
if (typeof meal === 'object'){ //checks that the input is of the correct data type
this._courses.desserts.push(meal);
} else {
console.log('Please input meal as object');
}
},
get courses(){
return this._courses;
},
addDishToCourse(courseName, dishName, dishPrice) {
if (Object.keys(this._courses).includes(courseName)){ //checks to make sure the input courseName matches one of the three course names defined within the _courses object.
const dish = {
name: dishName,
price: dishPrice,
};
this[courseName] = dish;
} else {
console.log('Please Enter a valid course name')
}
},
getRandomDishFromCourse(courseName) {
let dishes = this[courseName];
let 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');
return `Your randomly generated meal is- Appetizer: ${appetizer.name}, Main: ${main.name}, Dessert: ${dessert.name}. The total cost will be ${appetizer.price + main.price + dessert.price}`
}
}
menu.addDishToCourse('appetizers', 'soup', 4.5);
menu.addDishToCourse('appetizers', 'prawn cocktail', 5.5);
menu.addDishToCourse('appetizers', 'duck pate', 5.5);
menu.addDishToCourse('mains', 'roast beef', 10);
menu.addDishToCourse('mains', 'lamb shank', 11.5);
menu.addDishToCourse('mains', 'cheese and broccoli tart', 9.5);
menu.addDishToCourse('desserts', 'chocolate fudge brownie', 6);
menu.addDishToCourse('desserts', 'lemon cheesecake', 6.5);
menu.addDishToCourse('desserts', 'mango sorbet', 4);
meal = menu.generateRandomMeal();
console.log(meal);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment