Hello everyone, I hope everyone is doing well.
I have a quick question about the project Meal Maker from the JavaScript objects lesson: Meal Maker
First of all, here is my final working code:
const menu = {
_courses: {
appetizer: [],
main: [],
//sidedish: [],
dessert: [],
},
//Getter method
get appetizerGet () {
return this._courses.appetizer
},
get mainGet () {
return this._courses.main
},
get dessertGet () {
return this._courses.dessert
},
//Set Method
set appetizer (appetizer) {
this._courses.appetizer = appetizer;
},
set main (main) {
this._courses.main = main;
},
set dessert (dessert) {
this._courses.dessert = dessert;
},
//Assign Meals
addMeals (course, name, price) {
const dishAttributes = { name, price, }
if(course.toLowerCase() === 'appetizer') {
this._courses.appetizer.push(dishAttributes)
} else if (course.toLowerCase() === 'main') {
this._courses.main.push(dishAttributes)
} else if (course.toLowerCase() === 'dessert') {
this._courses.dessert.push(dishAttributes)
};
},
//Random Dish
getRandomDish (course) {
const appetizers = this.appetizerGet;
const mains = this.mainGet;
const desserts = this.dessertGet;
//correct use of Getter method?
if(course === 'appetizer') {
return appetizers[Math.floor(Math.random() * appetizers.length)]
} else if (course === 'main') {
return mains[Math.floor(Math.random() * mains.length)]
} else if (course === 'dessert')
return desserts[Math.floor(Math.random() * desserts.length)]
},
//Random Meals
generateRandomMeal () {
const appetizer = this.getRandomDish('appetizer');
const main = this.getRandomDish('main');
const dessert = this.getRandomDish('dessert');
const totalPrice = appetizer.price + main.price + dessert.price;
return `Welcome!! Today, we have ${appetizer.name} for your Appetizer, ${main.name} for your main dish and finally ${dessert.name} for your dessert. Thank you for coming, your total is ${Math.floor(totalPrice) }`;
},
}
//ASSIGNING MEALS
menu.addMeals('main', 'something', 20.00)
menu.addMeals('main', '1', 15.00)
menu.addMeals('main', '2', 9.99)
menu.addMeals('appetizer', '3', 8.99)
menu.addMeals('appetizer', '4', 5.00)
menu.addMeals('appetizer', '5', 2.99)
menu.addMeals('dessert', '6', 2.99)
menu.addMeals('dessert', '7', 3.99)
menu.addMeals('dessert', '8', 2.99)
const finalGeneratedDish = menu.generateRandomMeal()
console.log(finalGeneratedDish)
Second, the main focus of the question:
//Random Dish
getRandomDish (course) {
const appetizers = this.appetizerGet;
const mains = this.mainGet;
const desserts = this.dessertGet;
//correct use of Getter method?
if(course === 'appetizer') {
return appetizers[Math.floor(Math.random() * appetizers.length)]
} else if (course === 'main') {
return mains[Math.floor(Math.random() * mains.length)]
} else if (course === 'dessert')
return desserts[Math.floor(Math.random() * desserts.length)]
},
Does the code above use the getter Method as it was intended to do so or is there any other way to write this code as if in a shorter form? One more thing, I don’t see the step by step instructions telling us when to use the remaining 3 setter methods or did I miss something? I will really appreciate all the help!