No. 15 Meal Maker Course

https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/learn-javascript-objects/projects/meal-maker

I am wondering why my code when I do a test returns “mains is not defined” when attempting to add a course. Code is below and I would greatly appreciate a explanation. Thanks!

const menu = {
  _courses: {
    appetizers: [],
    get appetizers() {
      return appetizers;
    },
    set appetizers(appetizerIn){
      this.appetizers.push(appetizerIn);
    },
    mains: [],
    get mains() {
      return this.mains;
    },
    set mains(mainsIn){
      this.mains.push(mainsIn);
    },
    desserts: [],
    get desserts(){
      return this.desserts;
    },
    set desserts(dessertIn){
      this.desserts.push(dessertIn);
    }
  },
  get _courses(){
    return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts
    }
  },
  addDishToCourse(courseName, dishName, dishPrice){
    var dish = {dishName, dishPrice};
    this._courses[courseName].push(dish);
  },
  getRandomDishFromCourse(courseName){
    var dishes = courseName.map(function(n){
      return n;
    })
  }
}

I am going to preface this with the statement I am not brilliant with JavaScript.

That said.

I’m presuming that you’re calling menu.addDishToCourse(mains, "Chicken Madras", 8.99) and not menu.addDishToCourse("mains", "Chicken Madras", 8.99) because that’s how I’ve been able to replicate your error.

If that’s what you’re doing, you’re getting mains is not defined because there’s no variable available at that point called mains. You should be doing the latter: menu.addDishToCourse("mains", "Chicken Madras", 8.99)

Even then, you’ll get another error, because your object will at that point (at least when I tested it) end up in a neat little recursion of calling your _courses getter…

This seems to work... (click to reveal)
const new_menu = {
  courses: {
    appetisers: [],
    entrees: [],
    desserts: [],
  },
  get menu() {
    output = {
      our_appetisers: this.courses.appetisers,
      our_entrees: this.courses.entrees,
      our_desserts: this.courses.desserts,
    }
    return output;
  },
  addDishToCourse(courseName, dishName, dishPrice) {
    var dish = {dishName, dishPrice};
    this.courses[courseName].push(dish);
    return 0;
  }
}

… though, again, my knowledge of JS is not as good as some other languages. Also, I didn’t re-write your getRandomDishFromCourse() function as it looked unfinished to me in your original code…

1 Like

Your _courses{} object should contain 3 empty arrays - one for each course: “appetizers”, “mains” and “deserts”. It should not contain the getters and setters. They belong in the menu{} object, but not as part of the _courses{} object. Your basic structure should look like this:

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    dessserts: []
  },
  //getters, setters and methods
}

Also, I would use const instead of var. May as well get used to ES6. const also limits your variable to block scope which is all you need. var give the variable function scope which is uneccessary.
Hope this helps!

1 Like

Thanks for the help guys, after I restructured my code and Implemented what you suggested midlindner and put quotations around the _courses property, I got it to work. I appreciate the help!

2 Likes