JavaScript_ Meal Maker
15104 단어 testJavaScriptJavaScript
- 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);
Author And Source
이 문제에 관하여(JavaScript_ Meal Maker), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@sebely/JavaScript-Meal-Maker저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)