JS Mealmaker project

I created my object, but I can’t seem to get it to generate dishes; console.log is showing nothing when I run meal, or try to bring up menu._courses. I’ve got it to the point where no errors are generated, but something is wrong. Can someone take a look? Thanks.

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: []
  },
  get appetizers() {
    return this.appetizers;
  },
  set appetizers(appetizerIn) {
    this.appetizers = appetizerIn;
  },
  get mains() {
    return this.mains;
  },
  set mains(mainIn) {
    this.mains = mainIn;
  },
  get desserts() {
    return this.desserts;
  },
  set desserts(dessertIn) {
    this.desserts = dessertIn;
  },
  get courses() {
    return {
      appetizers: this.appetizers,
      mains:       this.mains,
      desserts:   this.desserts
    }
  },
  addDishToCourse (courseName, dishName, dishPrice){
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    this._courses[courseName].push(dish);
    console.log(dish);
  },
  getRandomDishFromCourse(courseName) {
    const dishes = this._courses[courseName];
    const randomDishIndex = 
    Math.floor(Math.random() * dishes.length);
    return dishes[randomDishIndex];
  },
  generateRandomMeal: function() {
    const appetizer = this.getRandomDishFromCourse('appetizers');
    const main = this.getRandomDishFromCourse('mains');
    const dessert = this.getRandomDishFromCourse('desserts');
    const totalPrice = appetizer.price + main.price + dessert.price;
    return `${appetizer.name}, ${main.name}, and ${dessert.name} come to a total of ${totalPrice}`;
  }
};

menu.addDishToCourse('appetizer', 'Bruschetta', 6.99);
//...
menu.addDishToCourse('main', 'Alfredo Pasta', 11.99);
//...
menu.addDishToCourse('dessert', 'Cake', 5.99);

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

That crashes with an error message did you post the right code?

In general though, if something isn’t behaving as you want then you’d go inspect the code responsible for that something.
For example, you say you can’t bring up menu._courses, does such a property exist? You could find out by printing menu and inspecting the output to see whether it’s there.
Each operation you carry out has some initial state and some final state, both of which you can inspect to determine whether the operation behaved correctly, and if so then you’d continue by inspecting whatever created the initial state, or if not then you’ve narrowed down the problem enough that you can look at documentation for the operation you’re carrying out.

I put ... between addDishToCourse calls, that might explain it. I pruned it back a bit for brevity.

> 37 |   this._courses[courseName].push(dish);

gets the error: Cannot read property 'push' of undefined, which tells me there’s something up with the _course object?

1 Like