Meal Maker 'Undefined' Errors

https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-learn-javascript-syntax-objects/projects/meal-maker

Experiencing multiple ‘undefined’ errors and unsure how to proceed.

let menu = {
  _courses: {
    _appetizers: [],
    _mains: [],
    _desserts: [],
  },
  get appetizers() {
    return this._appetizers
  },
  get mains() {
    return this._mains
  },
  get desserts() {
    return this._desserts
  },
  set appetizers(newApp) {
    this._appetizers = newApp
  },
  set mains(newMain) {
    this._mains = newMain
  },
  set desserts(newDessert) {
    this._desserts = newDessert
  },
  addDishToCourse(courseName, dishName, dishPrice) {
    let dish = { name: dishName, price: dishPrice }
    return this._courses[courseName].push(dish)
  },
  get courses() {
    return { appetizers: this._appetizers, mains: this._mains, desserts: this._desserts }
  },
  getRandomDishFromCourse(courseName) {
    let dishes = this._courses[courseName]
    return dishes[Math.floor(Math.random() * dishes.length)]
  },
  generateRandomMeal() {
    let randomApp = this.getRandomDishFromCourse('appetizers')
    let randomMain = this.getRandomDishFromCourse('mains')
    let randomDessert = this.getRandomDishFromCourse('desserts')
    let totalPrice = randomApp.price + randomMain.price + randomDessert.price

    return `Your meal is ${randomApp.name}, ${randomMain.name}, and ${randomDessert.name}. Your total for this meal is ${totalPrice}.`
  },
}

menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25)
menu.addDishToCourse('appetizers', 'Burrata Salad', 7.25)
menu.addDishToCourse('appetizers', 'Calamari', 6.25)

menu.addDishToCourse('mains', 'Beef', 10.25)
menu.addDishToCourse('mains', 'Chicken', 11.25)
menu.addDishToCourse('mains', 'Fish', 13.25)

menu.addDishToCourse('desserts', 'Flan', 3.25)
menu.addDishToCourse('desserts', 'Creme Brulee', 5.25)
menu.addDishToCourse('desserts', 'Chocolate Starwberries', 2.25)

let meal = menu.generateRandomMeal()

console.log(meal)
console.log(meal)
console.log(meal)
console.log(meal)
console.log(meal)
console.log(meal)

I would also like to replace many of the functions in the methods with my setter methods, but my brain is swimming.

That is undefined since there is not such property of menu.

return this._courses._appetizers

Your setters are not working, just now, but you might as well fix them, too.

this._courses._appetizers.push(newApp)

Use .push() in the setter, not = else you destroy the array. We can come back to this later.

We have getters which will be useful here.

  get courses() {
    return { 
      appetizers: this.appetizers, 
      mains: this.mains, 
      desserts: this.desserts 
    }
  },

We don’t return anything from a setter or setter-like method. Again, getters can be used for simplicity…

  addDishToCourse(courseName, dishName, dishPrice) {
    let dish = { 
      name: dishName, 
      price: dishPrice 
    }
    this[courseName].push(dish)  // getters are used here
  },

We can come back to this later, also.

Comment out the last line of your code after you make these changes, and log the menu object.

console.log(menu)

You should see something like this,

{ _courses: 
   { _appetizers: [ [Object], [Object], [Object] ],
     _mains: [ [Object], [Object], [Object] ],
     _desserts: [ [Object], [Object], [Object] ] },
  appetizers: [Getter/Setter],
  mains: [Getter/Setter],
  desserts: [Getter/Setter],
  addDishToCourse: [Function],
  courses: [Getter],
  getRandomDishFromCourse: [Function],
  generateRandomMeal: [Function] }

in the console. type and Enter:

 menu.courses

You should see something like this…

=> { appetizers: 
   [ { name: 'Caesar Salad', price: 4.25 },
     { name: 'Burrata Salad', price: 7.25 },
     { name: 'Calamari', price: 6.25 } ],
  mains: 
   [ { name: 'Beef', price: 10.25 },
     { name: 'Chicken', price: 11.25 },
     { name: 'Fish', price: 13.25 } ],
  desserts: 
   [ { name: 'Flan', price: 3.25 },
     { name: 'Creme Brulee', price: 5.25 },
     { name: 'Chocolate Strawberries', price: 2.25 } ] }

Now to get the setters working.

Above I suggested you rewrite the setters using .push(). Next we will revise the addDishToCourse() method…

  addDishToCourse(courseName, dishName, dishPrice) {
    let dish = { 
      name: dishName, 
      price: dishPrice 
    }
    this[courseName] = dish    //  uses the setters
  },

Note the assignment. This will invoke the setter.

That leaves just the other two methods to review and revise. Let us know when you’re ready to tackle those.

1 Like

I was able to fix and figure out the rest of my errors from there. Thanks!

1 Like