Meal Maker Syntax Error

I’m working on the JavaScript Meal Maker project, and am getting a syntax error that I haven’t been able to figure out. My code is listed below:

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) {
    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) {
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    return 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 appetizers = this.getRandomDishFromCourse('appetizers');
    const mains = this.getRandomDishFromCourse('mains');
    const desserts = this.getRandomDishFromCourse('desserts');
    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', 'salad', 4.00);
menu.addDishToCourse('appetizers', 'tomatoes', 6.00);
menu.addDishToCourse('appetizers', 'Poke', 9.00);

menu.addDishToCourse('mains', 'trout', 21.00);
menu.addDishToCourse('mains', 'cauliflower', 19.00);
menu.addDishToCourse('mains', 'stir fry', 18.00);

menu.addDishToCourse('desserts', 'cheesecake', 5.00);
menu.addDishToCourse('desserts', 'tart', 4.00);
menu.addDishToCourse('desserts', 'flan', 6.00);

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

There error is occurring on the ‘addDishToCourseLine’, but I don’t know what the error is. Here is the exact syntax error:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:32
  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)

I would appreciate any insight or help. Thank you!

You’ve just missed out the comma after the get courses() getter, and also after the addDishToCourse() function. That should fix your issue I think!

I changed up the commas with no luck?

You probably need to comment out chunks of your code and see what’s not throwing an error to begin with.

I personally prefer working on this type of troubleshooting with a text editor than can simulate the console output.

image

Using VS Code, which has some real lifesaver features, we can see it’s flagging up addDishToCourse as an error. This is because after the curly bracket on line 31 you are missing a comma. Since these are all functions in an object, they need to be separated by commas. There are a couple other issues in this code, if you read your error messages you’ll see the issues, but if you add a comma here on line 31 and then another on line 38 that will fix this issue.

1 Like

Thank you so much! It was helpful to try this in VS Code.

1 Like