Meal Maker

Meal maker
https://www.codecademy.com/paths/build-web-apps-with-react/tracks/bwa-javascript-iterators-objects-and-classes/modules/learn-javascript-objects/projects/meal-maker

Hi can’t see what I’m doing wrong. Thought I could try this forum?

heres my code:

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(appetizersIn) {
      this._courses.appetizers = appetizersIn;
    },
    set mains(mainsIn) {
      this._courses.mains = mainsIn;
    },
    set desserts(dessertsIn) {
      this._courses.desserts = dessertsIn;
    }
  },
  get courses() {
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts
    };
  },
  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: this.dishName,
      price: this.dishPrice
    }
    this._courses[courseName].push(dish);
  },
  getRandomDishFromCourse(coursName) {
    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 + desser.price;
    return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name}, The total price is ${totalPrice}.`;
  }
};

menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25);
menu.addDishToCourse('appetizers', 'Rökt lax', 5.44);
menu.addDishToCourse('appetizers', 'Liten räka', 2);

menu.addDishToCourse('mains', 'Mammas Köttbullar', 10.33);
menu.addDishToCourse('mains', 'Dagens paj', 8.77);
menu.addDishToCourse('mains', 'Dagens burgare', 7.66);

menu.addDishToCourse('desserts', 'Glass', 4);
menu.addDishToCourse('desserts', 'Kaka', 3);
menu.addDishToCourse('desserts', 'Banan', 10.22);

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

Thankful for help

Then we should gain insight what is wrong. The first logic step would be to do:

get appetizers() {
      console.log(this._courses);
      return this._courses.appetizers;
    },

we can indeed see that this._courses is indeed undefined. But why?

looking at the courses object:

{
    appetizers: [],
    mains: [],
    desserts: [],
}

we see no _courses property.

2 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.