const menu = {
_courses: {
appetizers: [],
mains: [],
deserts: [],
},
get appetizers(){
return this._courses.appetizers;
},
get mains() {
return this._courses.mains;
},
get deserts(){
return this._courses.deserts;
},
set appetizers(appetizers){
this._courses.appetizers = appetizers;
},
set mains(mains){
this._courses.mains = mains;
},
set deserts(deserts){
this._courses.deserts = deserts;
},
get courses(){
return {
appetizers: this.appetizers,
mains: this.mains,
deserts: this.deserts,
};
},
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 = (dishes[Math.floor(Math.random() * dishes.length)]);
return dishes[randomIndex];
},
generateRandomMeal(){
const appetizer = this.getRandomDishFromCourse('appetizers');
const main = this.getRandomDishFromCourse('mains');
const desert = this.getRandomDishFromCourse('deserts');
const totalPrice = appetizer.price + main.price + desert.price;
return `Your meal is ${this._courses.appetizers.name} , ${this._courses.mains.name} and ${this.courses.deserts.name} and the total cost is ${totalPrice}`;
},
};
menu.addDishToCourse('appetizers','appetizer1' , 1.0 );
menu.addDishToCourse('appetizers','appetizer2' , 2.0 );
menu.addDishToCourse('appetizers','appetizer3' , 3.0 );
menu.addDishToCourse('mains','mains1' , 1.0 );
menu.addDishToCourse('mains','mains2' , 2.0 );
menu.addDishToCourse('mains','mains3' , 3.0 );
menu.addDishToCourse('deserts','deserts1' , 1.0 );
menu.addDishToCourse('deserts','deserts2' , 2.0 );
menu.addDishToCourse('deserts','deserts3' , 3.0 );
const meal = menu.generateRandomMeal();
console.log(meal);
if I add a console.log to your code:
getRandomDishFromCourse(courseName){
const dishes = this._courses[courseName];
const randomIndex = (dishes[Math.floor(Math.random() * dishes.length)]);
console.log(dishes, randomIndex);
return dishes[randomIndex];
},
I see that your randomIndex
variable does not contain a random index, but rather you already use the random index to get a random value.
This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.