Hello,
I’m struggling a bit with the mentioned project. The code is below, I also have a few questions.
First of all: is it ok to create a new topic for these projects? I was considering posting in others’ threads, however, it could make conversation there more convoluted. What’s the most common and desirable practice?
Moving to the code itself; I’ve written it with the help of an explainer video, however, it might not be 100% comparable, as I’ve tried to have some degree of understanding what I do.
The error I get is below, what baffles me is the fact that the error refers to the second line of the code, which is similar to the one above.
My last question is the use of the gets and sets, why exactly are they necessary?
const main = this.getRandomDishFromCourse('mains'),
^^^^^
SyntaxError: Unexpected token const
``
``
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 mains(mains) {
this._courses.mains = mains;
},
set appetizers(appetizers) {
this._courses.appetizers = appetizers;
},
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} and the price is ${totalPrice}.`;
}
};
menu.addDishToCourse('appetizers', 'Bruschetta', 5);
menu.addDishToCourse('appetizers', 'Tatar', 15)
menu.addDishToCourse('appetizers', 'Onion Rings', 7.65)
menu.addDishToCourse('mains', 'Steak', 25);
menu.addDishToCourse('mains', 'Chicken Breast', 19);
menu.addDishToCourse('mains', 'Pizza', 12);
menu.addDishToCourse('desserts', 'Apple Pie', 9.90);
menu.addDishToCourse('desserts', 'Brownie', 11.90);
menu.addDishToCourse('desserts', 'Ice Cream', 7.90);
const meal = menu.generateRandomMeal();
console.log(meal);