Price is undefined

hi, i don’t know why my “price is undefined” when I run my code - can anyone help?

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) {
    return this._courses.appetizers = appetizers;
  },
   set mains(mains) {
    return this._courses.mains = mains;
  },
   set desserts(desserts) {
    return this._courses.desserts = desserts;
  }, 
  get courses() {
  return {
    appetizers: this.appetizers, 
    mains: this.mains, 
    desserts: this.desserts,
    };
  }, 
  addDishToCourse (courseName, dishName, dishPrice) {
    let dish = {
      name: dishName, 
      price: dishPrice, 
    };
    return this._courses[courseName].push(dish); 
  },
    getRandomDishFromCourse(courseName) {
      const dishes = courseName;
      const randomIndex = Math.floor(Math.random * dishes.length);
      return dishes[randomIndex]; 
    }, 
    generateRandomMeal() {
      const appetizer = this.getRandomDishFromCourse('appetizer');
      const main = this.getRandomDishFromCourse('main');
      const dessert = this.getRandomDishFromCourse('dessert');
      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', 'Caesar Salad', 4.25);
menu.addDishToCourse('appetizers', 'soup', 4.00);
menu.addDishToCourse('appetizers', 'nachos', 3.25);

menu.addDishToCourse('mains', 'steak', 9.25);
menu.addDishToCourse('mains', 'pizza', 8.25);
menu.addDishToCourse('mains', 'pasta', 5.25);

menu.addDishToCourse('desserts', 'cake', 4.30);
menu.addDishToCourse('desserts', 'ice cream', 1.25);
menu.addDishToCourse('desserts', 'popsicle', 2.25);

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




also, now my length is undefined? but i defined the “dishes” right above this line. can anyone help?

getRandomDishFromCourse(courseName) {
      const dishes = this._courses[courseName]; 
      const randomIndex = Math.floor(Math.random * dishes.length);
      return dishes[randomIndex]; 
    }, 

If you look at the structure of the object, the context for this is menu, not _courses.

appetizers: this._courses.appetizers

ah, thank you - but what about the length not being defined for dishes?

    getRandomDishFromCourse (courseName) {
      const dishes = this._courses[courseName] 
      const randomIndex = Math.floor(Math.random()*dishes.length);
      return dishes[randomIndex]; 
    },

i don’t know why I am receiving an TypeError stating that the system cannot read property length of undefined.

Before the lines that begin to add dishes, insert a console.log(menu.courses) and see it gives you an object with three properties, all empty arrays.

It showed three empty arrays, but I’m confused about why my dishes didn’t change when i wrote my code beginning “const” that sets the dishes ): i’m sorry i have so many questions, I just don’t understand why dishes doesn’t change as I’m adding the courses to these arrays.

That’s good. It’s what we expect to see. Now (without running all the lines at the end, just comment them out) run addDishToCourse on one appetizer, then log that…

console.log(menu.courses.appetizers)

Does it return an array with your new item?

yes! it returns this

[ { name: 'Caesar Salad', price: 4.25 } ]

this is the code:

menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25);
console.log(menu.courses.appetizers)

why would dishes not be defined then?

Can’t say, just now. Let’s keep adding dishes, one to each of the other courses, and log them out, separately, to see if they all worked. Then log out the courses object,

console.log(menu.courses)

Also for now, comment out all the getters and setters except get courses. We’re not using them right now, anyway.

Add another dish to each course and log them out again to see if they each have two items. Log the courses object, again, too.

Hi, thank you so much for all of your help. I see the issue is that this portion of my code results in undefined.

 addDishToCourse (courseName, dishName, dishPrice) {
   console.log(menu.courses)
    let dish = {
      name: dishName, 
      price: dishPrice, 
    };
    console.log(this.courses[courseName]);
    return this.courses[courseName].push(dish); 
  },
    getRandomDishFromCourse(courseName) {
      **const dishes = this.courses[courseName];**
      const randomIndex = Math.floor(Math.random()* dishes.length);
      return dishes[randomIndex]; 
      },

i was able to log this two lines into the console:

 console.log(this.courses[courseName]);
    return this.courses[courseName].push(dish); 
  },

above the getRandomDishFromCourse(courseName) { method. it seems that this method is not taking into account any of the methods from above - why?

Again, can’t say, just yet. All the logging should be done outside of the menu object, down where you are adding dishes.

Do not return. Just push the new dish. Be sure to push to the correct object… this._courses[courseName]. There is no courses data property, pe se, only the backing variable.

We use the getter in this line…

console.log(menu.courses.appetizers)
1 Like

Ahhhhh!!! thank you times a billion!!! i followed your instructions and started logging each line outside the menu step-by-step. I had to ultimately alter my generateRandomMeal() method & also remove the return, and fix the object to include _ before courses. The code all works, and I appreciate your help SO much! Despite these trying times, this was a small victory and made my day. Thank you again!!!

2 Likes

That’s great! You’re welcome. Happy coding!

2 Likes