Meal Maker - cant find whats the error "TypeError: menu.addDishToCourse is not a function"

Hey guys
I’ve been combing this desert for an hour now and still cant figure out why been given an error rather then run it
I’m testing to see if my functions worked but rather then execute the functions im getting a type error

Currently at step 12 of the exercise

here’s a screencap of the error given

below is my code


let  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 (appetizer){
      this._courses.appetizers = appetizer;
    },
    set mains (main){
      this._courses.mains = main;
    },
    set desserts (dessert){
      this._courses.desserts = dessert;
    },
    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 randNum = Math.floor(Math.random()*dishes.length);
      return dishes[randNum];
    },
    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 `The order of ${appetizer} ${main} and ${dessert} will cost a total of ${totalPrice}`;
    }
 } 
};

console.log(menu._courses);

menu.addDishToCourse('appetizers','Eggroll',2);
addDishToCourse('mains','General Tzo Chicken',10);
addDishToCourse('desserts','Furtune cookies',1);

console.log(menu._courses);

Thnks!

you added the methods (like addDishToCourse ) to the _courses object, so either you need to do:

menu._courses.addDishToCourse()

or you need to move the method from the courses object to the menu object

Personally, I would to the latter

step 7:" Inside the menu object, we are going to create a method called *.addDishToCourse*() which will be used to add a new dish to the specified course on the menu.

The method should take in three parameters: the courseName , the dishName , and the dishPrice ."

the instructions state for the functions to be set inside the menu object
im trying to check them from outside the menu obj

shouldnt menu.addDishToCourse(); work outside the object?

you added this method to _courses object, not menu object like instructed

:thinking: :thinking:
I repositioned my functions outside _courses to the menu scope

now im getting this error:

"/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:7
return this._courses.appetizers;
^

TypeError: Cannot read property ‘appetizers’ of undefined
at Object.get appetizers [as appetizers] "

I checked the walkthrough and devs wrote it the same
what am i missing here?

repositioned code below:



let  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 (appetizer){

      this._courses.appetizers = appetizer;

    },

    set mains (main){

      this._courses.mains = main;

    },

    set desserts (dessert){

      this._courses.desserts = dessert;

    },

    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 randNum = Math.floor(Math.random()*dishes.length);

    return dishes[randNum];

  },

  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 `The order of ${appetizer} ${main} and ${dessert} will cost a total of ${totalPrice}`;

    }

};

console.log(menu._courses);

menu.addDishToCourse('appetizers','Eggroll',2);

//addDishToCourse('mains','General Tzo Chicken',10);

//addDishToCourse('desserts','Furtune cookies',1);

console.log(menu._courses);

now you have a similar problem:

  _courses:{ 

    appetizers:[],

    mains:[],

    desserts:[],

    get appetizers () {

      return this._courses.appetizers;

    },

the getter is part of the courses object, which means to get the property would require you to use this.appetizers (given this references the current _courses object

however, this would lead to recursion, so the properties would need an underscore prefix

or, you could move the getters from _courses to menu object, like you did with the methods. then we can use menu.appetizers to get the nested underlying property

oh jeez
finally it worked
i misunderstood the getter setter step and thought they all plus functions should be inside _courses
:man_facepalming: :man_facepalming:

wish they lessons had more clear examples on accessing nested attribute with relation to functions getters this etc
sure mixes me up

also possible, but I don’t think its the best solution.

Besides what the lesson should or shouldn’t do, the best way to learn is to analyze things for yourself. Considering pro’s and cons, trail things (on occasion), this will teach you more in the long run then a course or explanation.

Blockquote
trail things (on occasion), this will teach you more in the long run then a course or explanation.
Blockquote

:thinking: :thinking: what those it mean? could u elaborate with an example parhaps?

When I follow the instructions, i.e.

I get this:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:51
addDishToCourse(courseName, dishName, dishPrice){
^^^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

This project has had me stumped for 2 days now. Very frustrating and demoralising, the error messages are not at all helpful !!! I am now going to just follow along the video, which I dislike.

edit: for future visitors who read this, play around with where you put the commas!!

understanding where you place the comma’s and what purpose they serve is also very important

1 Like

Totally agree, and I’ll be paying especial attention form now on!