Learn Java Script: Objects Meal Maker


#1

Hey, I would be glad if someone could help me, so.
At the moment I can not get any further here:
https://www.codecademy.com/courses/learn-javascript-objects/projects/meal-maker?course_redirect=introduction-to-javascript

I think that I have done all right. But if I want to run my programm the console prints this error:

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

And here is my code:

const menu = {
  _courses: {},
   get courses() {
     return {
    appetizers: this._courses.appetizers, 
    mains: this._courses.mains,
    desserts: this._courses.desserts
     }
   },
    appetizers: [],
    mains: [],
    desserts: [],
   set appetizers(newAppetizers) {
     
   },
   get appetizers() {
     
   },
   set mains(newMain) {
     
   },
    get mains() {
     
   },
   set desserts(newDesserts) {
     
   },
   get desserts() {
     
   },
  addDishToCourse(courseName, dishName, dishPrice) {
    let dish = {
      name: dishName,
      price: dishPrice
    },
    
    this._courses[courseName].push(dish);
  },
 getRandomDishFromCourse(courseName) {
   const dishes = this._courses[courseName];
   const randomIndex = Math.floor(Math.random() * dishes.length);
 },
 generateRandomMeal() {
   const appetizer = this.getRandomDishFromCourse('appetizers');
   const desserts = this.getRandomDishFromCourse('desserts');
   const mains = this.getRandomDishFromCourse('mains');
   const totalPrice = appetizer.price + mains.price + desserts.price;
   
   return `Your meal is ${appetizer.name}, ${main.name}, ${desserts.name}. The price is $${totalPrice}.`;
 }
};

menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25);
menu.addDishToCourse('appetizers', 'Pizza bun', 2.50);
menu.addDishToCourse('appetizers', 'Cola', 1.50);

menu.addDishToCourse('mains', 'Pizza', 2.30);
menu.addDishToCourse('mains', 'Fish and Chips', 2.00);
menu.addDishToCourse('mains', 'Noodles', 3.50);

menu.addDishToCourse('desserts', 'Icecream', 0.50);
menu.addDishToCourse('desserts', 'Pudding', 1.50);
menu.addDishToCourse('desserts', 'Strawberrys', 0.50);

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

Thank you :slight_smile:


#2

This may not have to do with the problem, but just in case, let’s move it to below all the other attributes and setters.

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

This line is a bit tricky…

this._courses[courseName].push(dish);

We should rightly be calling the setter for the course in question, and have that setter push the item to the array.

this._courses[courseName] = dish;

Now the setters will look like this…

get appetizers () {
    return this._appetizers
},
set appetizers (appetizer) {
    this.appetizers.push(appetizer);
},

Note that the setter above calls the getter for the actual attribute.


#3

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