Meal Maker .push() not working

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 appetizers(appetizers) {
this._courses.appetizers = appetizers;
},
set mains(mains) {
this._courses.mains = mains;
},
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._course[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}, and ${dessert.name}, and the total price is ${totalPrice} ;
}
};

menu.addDishToCourse(‘appetizers’, ‘salad’, 4.00);
menu.addDishToCourse(‘appetizers’, ‘wings’, 4.50);
menu.addDishToCourse(‘appetizers’, ‘fries’, 5.00);

menu.addDishToCourse(‘mains’, ‘steak’, 10.00);
menu.addDishToCourse(‘mains’, ‘tofu’, 11.00);
menu.addDishToCourse(‘mains’, ‘chicken’, 9.00);

menu.addDishToCourse(‘desserts’, ‘icecream’, 6.50);
menu.addDishToCourse(‘desserts’, ‘cake’, 7.00);
menu.addDishToCourse(‘desserts’, ‘brownie’, 8.00);

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

Hello, and welcome to the forums

You have a small typo. In part of your code, you refer to this._courses and in another this._course

2 Likes

the same problem. Checked everything … but doesn’t work

return this._courses [courseName] .push (dish);
^
TypeError: Cannot read property ‘push’ of undefined
at Object.addDishToCourse

const menu = {
  _courses: {
    appertizers: [],
    mains: [],
    deserts: [],
  },
  get appertizers() {
  return this._courses.appertizers;
  },
  get mains() {
  return this._courses.mains;
  },
  get deserts() {
  return this._courses.deserts;
  },
  set appertizers(appertizers) {
    return this._courses.appertizers = appertizers;
  },
  set mains(mains) {
    return this._courses.mains = mains;
  },
  set deserts(deserts) {
    return this._courses.deserts = deserts;
  },
  get courses() {
    return {
      appertizers: this.appertizers,
      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 = Math.floor(Math.random() * dishes.length);
    return dishes[randomIndex];
  },
  generateRandomMeal() {
    const appertizer = getRandomDishFromCourse('appertizers');
    const main = getRandomDishFromCourse('mains');
    const desert = getRandomDishFromCourse('deserts');
    const totalPrice = appetizer.price + main.price + desert.price;
    return `The meal is ${appetizer.name}, ${main.name} and desert ${desert.name}. Total price ${totalPrice}`;
  }
};

menu.addDishToCourse('appertizer', 'salad', 8,50);
menu.addDishToCourse('appertizer', 'salad2', 7,50);
menu.addDishToCourse('appertizer', 'salad3', 6,50);
menu.addDishToCourse('main', 'steak', 10,50);
menu.addDishToCourse('main', 'steak2', 11,50);
menu.addDishToCourse('main', 'steak3', 12,50);
menu.addDishToCourse('desert', 'ice cream', 5,50);
menu.addDishToCourse('desert', 'ice cream2', 4,50);
menu.addDishToCourse('desert', 'ice cream3', 3,50);

// const meal = meal.getRandomMeal();
// console.log(meal);

The main issue here is your generateRandomMeal() function. It’s calling getRandomDishFromCourse(), which is a method inside of menu, so it needs to be called via Object.method().
As generateRandomMeal() is also a method of meal, you can use this:
this.getRandomDishFromCourse(courseName)

You also have typos in rows 53 - 61, to save to the meals to the corerct course they all need to be pluralized, as your setter functions are all pluralized. You’ve also switched between appetizers and appertizers (note the ‘r’), so they’re not talking to each other.

Here’s a fix for generateRandomMeal():

generateRandomMeal() {
    const appetizer = this.getRandomDishFromCourse('appertizers');
    const main = this.getRandomDishFromCourse('mains');
    const desert = this.getRandomDishFromCourse('deserts');
    const totalPrice = appetizer.price + main.price + desert.price;
    return `The meal is ${appetizer.name}, ${main.name} and desert ${desert.name}. Total price ${totalPrice}`;
  }

When I made these changes, your code worked fine

1 Like