JavaScript_ Meal Maker

  • JS를 활용하여 랜덤 주문메뉴 만들기
  • getter와 setter, object를 이용하여 만들기
const menu = {
 _courses: {
   _appetizers: [],
   _mains: [],
   _desserts: [],

   get appetizers() {
     return this._appetizers;
   }, 
   set appetizers(apptdish) {
     this._appetizers = apptdish;
   },
   get mains() {
     return this._mains;
   },
   set mains(maindish) {
     this._mains = maindish;
   },
   get desserts() {
     return this._desserts;
   },
   set desserts(dessertdish) {
     this._desserts = dessertdish;
   },
 },
 
 get courses() {
   return {
     appetizers: this._courses.appetizers,
     mains: this._courses.mains,
     desserts: this._courses.desserts,
   };
 },

 addDishToCourse(courseName, dishName, dishPrice) {
   const dish = {
     name: dishName,
     price: dishPrice,
  };

   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 `Today menu : ${appetizer.name}, ${main.name}, ${dessert.name}, Price is ${totalprice} dollars`;
  }
};

  menu.addDishToCourse('appetizers', 'Salad', 8);
  menu.addDishToCourse('appetizers', 'Bread', 5);
  menu.addDishToCourse('appetizers', 'Fries', 4);
  
  menu.addDishToCourse('mains', 'Steak', 13);
  menu.addDishToCourse('mains', 'Pasta', 11);
  menu.addDishToCourse('mains', 'Pizza', 12);
  
  menu.addDishToCourse('desserts', 'Coffee', 5);
  menu.addDishToCourse('desserts', 'Ice cream', 4);
  menu.addDishToCourse('desserts', 'Cake', 6);

let meal = menu.generateRandomMeal();
console.log(meal);

좋은 웹페이지 즐겨찾기