Meal Maker

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._courses[courseName];
  const randomIndex = Math.floor(Math.random() * dishes.lenght);
  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}. The price is $${totalPrice}.`;
}

};

menu.addDishToCourse(‘appetizers’, ‘Fried Pickles’, 8.60)
menu.addDishToCourse(‘appetizers’, ‘Ranch Dip’, 5.35)
menu.addDishToCourse(‘appetizers’, ‘Garlic Bread’, 6.66)
menu.addDishToCourse(‘mains’, ‘Steak Tips’, 38.40)
menu.addDishToCourse(‘mains’, ‘Salmon’, 12.95)
menu.addDishToCourse(‘mains’, ‘Eggplant Parm’, 17.99)
menu.addDishToCourse(‘desserts’, ‘Ice Cream’, 7.30)
menu.addDishToCourse(‘desserts’, ‘TerribleSue’, 8.95)
menu.addDishToCourse(‘desserts’, ‘Coffee’, 1.95)

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


I keep getting a TypeError: “Cannot read property ‘price’ of undefined” I don’t understand where I went wrong here. Can someone please point out what my mistake is, and either explain it to me or point me in the direction of the proper reference material.

If it says “Cannot read XY of undefined” it means that the item you’re appending the key to (the property price in this case) is undefined.
For example here:

Either appetizer, main or dessert (or all of them) is undefined.
Log all of these variables in the generateRandomMeal method to find out, which is undefined.
Then you can check whether you have a spelling error or which other method you’re getting the item from doesn’t return the correct result.

For future posts: Please format your code.

length is misspelled in the getRandomDishFromCourse function.

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._courses[courseName];

      const randomIndex = Math.floor(Math.random() * dishes.lenght);

      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}. The price is $${totalPrice}.`;

    }

  };

  menu.addDishToCourse('appetizers', 'Fried Pickles', 8.60)

  menu.addDishToCourse('appetizers', 'Ranch Dip', 5.35)

  menu.addDishToCourse('appetizers', 'Garlic Bread', 6.66)

  menu.addDishToCourse('mains', 'Steak Tips', 38.40)

  menu.addDishToCourse('mains', 'Salmon', 12.95)

  menu.addDishToCourse('mains', 'Eggplant Parm', 17.99)

  menu.addDishToCourse('desserts', 'Ice Cream', 7.30)

  menu.addDishToCourse('desserts', 'TerribleSue', 8.95)

  menu.addDishToCourse('desserts', 'Coffee', 1.95)

  const meal = menu.generateRandomMeal();

 console.log(meal);

I still can’t figure where my error is coming from, and I don’t know how to format the code as asked. I am totally clueless right now.

Hmm, I tried to provide you with a technique to find the source of the error.
@janbazant1107978602 then pointed out the line with the error directly. The typo he spotted is still in your code…

smashed head against keyboard

I thought I fixed that, but I never saved it! I use VS Code and Quokka to do these and just copy and paste it into the program after. Problem solved, thank you @janbazant1107978602 & @mirja_t

1 Like
const menu = {
  _course: {
    appetizers: [],
    mains: [],
    desserts: []
  },
  get appetizers() {
    return this._course.appetizers;
  },
  set appetizers (appetizers) {
    this._course.appetizers = appetizers;
  },
  get mains() {
    return this._course.mains;
  } ,
  set mains(mains) {
    this._course.mains = mains;
  },
  get desserts() {
    return this._course.desserts;
  },
  set desserts(desserts) {
    this._course.desserts = desserts;
  },
  get course() {
    return{
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts
    };
  },
  addDishToCourse (courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    this._course[courseName].push(dish);
  },
  getRandomDishFromCourse(courseName){
    const dishes = this._course[courseName];
    const randomIndex = Math.floor(Math.random()* dishes.lenght);
    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}, The price is $${totalPrice}.`;
  }
};

menu.addDishToCourse ('appetizers', 'PannerTikka', 5.66);
menu.addDishToCourse ('appetizers', 'Veg 365', 4);
menu.addDishToCourse ('appetizers', 'Sheek Kebab', 6.50);
menu.addDishToCourse ('mains', 'Veg Korma', 10.50);
menu.addDishToCourse ('mains', 'Veg Handi Masala', 15);
menu.addDishToCourse ('mains', 'Bhindi Masala', 9.75);
menu.addDishToCourse ('desserts', 'Mango Lassi', 6.80);
menu.addDishToCourse ('desserts', 'Jalebi', 4.50);
menu.addDishToCourse ('desserts', 'Gulab Jamun', 8.80);

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

Same Error can’t find where I am wrong

Same problem as post 3:

1 Like

Do you teach ??

would like to be your student.