Not sure why my code is bringing up an error message - JavaScript

I’ve tweaked my code once more but still error messages are occuring. This link is Meal Maker and the error message now is:
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:34
menu.addDishToCourse (courseName, dishName, dishPrice) {
^
SyntaxError: Unexpected token .
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)

const menu = {
  _courses : {
    appetizers : [],
    mains : [],
    dessert : [],
  },
    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,
      }
    },

  menu.addDishToCourse (courseName, dishName, dishPrice) {
  (!(courseName in this._courses)); 
    {
      this._courseName[courseName] }
    
    const dish = {
      courseName : [],
      name: dishName,
      price: dishPrice,
    }
  };
  
  this._courses[courseName].push(dish);

  menu.getRandomDishFromCourse(courseName) {
    const dishes = this._courses[courseName];
    const randomIndex = Math.floor(Math.random() * dishes.length);
    return dishes[randomIndex];
    },
menu.generateRandomMeal() {
    const appetizer 
    = this.getRandomDishFromCourse('appetizer');
    const main 
    = this.getRadomDishFromCourse('main');
    const dessert 
    = this.getRandomDishFromCourse('dessert');
    const totalPrice = appetizer.price + main.price + dessert.price;
    return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name}. The price is $${totalPrice}.`;
  },
  };


menu.addDishToCourse('appetizer', 'salad', 4.00);
menu.addDishToCourse('appetizer', 'duck', 4.50);
menu.addDishToCourse('appetizer', 'soup', 3.50);

menu.addDishToCourse('main', 'steak', 24.00);
menu.addDishToCourse('main', 'fish', 16.00);
menu.addDishToCourse('main', 'pork', 20.00);

menu.addDishToCourse('dessert', 'pie', 7.00);
menu.addDishToCourse('dessert', 'icecream', 6.00);
menu.addDishToCourse('dessert', 'cheesecake', 7.00);

 const meal = 
 menu.generateRandomMeal()

console.log(meal);





My code below is bringing up this error message:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:45
this._courses[courseName].push(dish);
^
SyntaxError: Unexpected token .
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)

const menu = {
 _courses : {
   appetizers : [],
   mains : [],
   dessert : [],
 },
   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) {
 (!(courseName in this._courses)); 
   {
     this._courseName[courseName] 
   
   const dish = {
     courseName : [],
     name: dishName,
     price: dishPrice,
 this._courses[courseName].push(dish);
   },
 },
 

 getRandomDishFromCourse(courseName) {
   const dishes = this._courses[courseName];
   const randomIndex = Math.floor(Math.random() * dishes.length);
   return dishes[randomIndex];
   },
generateRandomMeal() {
   const appetizer 
   = this.getRandomDishFromCourse('appetizer');
   const main 
   = this.getRadomDishFromCourse('main');
   const dessert 
   = this.getRandomDishFromCourse('dessert');
   const totalPrice = appetizer.price + main.price + dessert.price;
   return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name}. The price is $${totalPrice}.`;
 },
};


menu.addDishToCourse('appetizer', 'salad', 4.00);
menu.addDishToCourse('appetizer', 'duck', 4.50);
menu.addDishToCourse('appetizer', 'soup', 3.50);

menu.addDishToCourse('main', 'steak', 24.00);
menu.addDishToCourse('main', 'fish', 16.00);
menu.addDishToCourse('main', 'pork', 20.00);

menu.addDishToCourse('dessert', 'pie', 7.00);
menu.addDishToCourse('dessert', 'icecream', 6.00);
menu.addDishToCourse('dessert', 'cheesecake', 7.00);

const meal = 
menu.generateRandomMeal()


console.log(meal);





I was tweaking the code for something like 10 hours yesterday and I’ve tried googling, asking on Reddit but to no avail!

Hi Vickyrai,
Would you mind pasting the link to the task? I don’t understand the addDishToCourse method at all as you’ve written it and it would be useful to see it in the context of the requirements.

One immediate thing I think I see is that I think you mean to have this._courses[courseName].push(dish) outside of the dish object…?

Hi @tera0976883653. Thanks so much! The link is : Meal Maker

Hey Vickyrai,
Woo, that was a tricky one. Firstly, I think you got yourself into the weeds with the addDishToCourse method, just take this one:

    addDishToCourse (courseName, dishName, dishPrice) {
      const dish = {
        name: dishName,
        price: dishPrice,
      };
        this._courses[courseName].push(dish);
    },

which does what you want and then never look back.

Basically all of the rest of the problem is typos (one ‘random’ is misspelled as ‘radom’, one underscore is missing in the setters, etc.) and make sure that you’re really consistent with when you use ‘appetizer’ and ‘appetizers’. In the _courses object, for example, that should be ‘desserts’, not ‘dessert’. Go through and think each time you use one of those words, ‘Am I referring to the course or an dish?’. If you’re referring to the course, it should be one of “appetizers”, “mains” or “desserts”; if you’re referring to a dish, it should be “appetizer”, “main” or “dessert”.

An example of this is the setters - they should be as follows:

set appetizers(appetizer) {
//appetizers is the course, appetizer is the dish
  this._courses.appetizers = appetizer;
},

I remember having massive problems with this one and I had to watch the walkthrough for one silly mistake I’d made. It’s a bit typo-prone, this project, and then everything breaks and you don’t know why…! If you have Visual Studio Code on your machine, maybe try doing this on there as well as it’ll often show you exactly where problems are in your code and why. Good luck (obviously if you find it’s all still broken, paste again…!).

1 Like

@tera0976883653 Thanks so much for your reply. I’ve sifted through the code and I don’t think there’s anymore typo’s but now there’s two more error messages. I’ll post in another, new, thead.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.