Meal maker

Hello friends,
I can’t finish the meal maker project.
The problem is the addDishCourse function that does not recognize my push to the arrow.
Could you help me to fix this?

let menu = {
    _courses: {
        appetizers: [],
        mains: [],
        desserts: [],
        get appetizers() {

        },
        set appetizers(appetizerIn) {

        },
        get mains() {

        },
        set mains(mainsIn) {

        },
        get desserts() {

        },
        set desserts(dessertsIn) {

        },

    },
    get _courses() {
        return { appetizers: this.appertizers, mains: this.mains, desserts: this.desserts }
    },
    addDishToCourse(courseName, dishName, dishPrice) {
        let dish = { name: dishName, price: dishPrice }
        this._courses[courseName].push(dish); 
    },
    getRandomDishFromCourse(courseName) {
        let dishes = this._courses[coursename];
        const randomIndex = Math.floor(math.random() * dishes.length)
    },
    generateRandomMeal: function () {
        var appetizer = this.getRandomDishFromCourse('appetizers');
        var main = this.getRandomDishFromCourse('main');
        var dessert = this.getRandomDishFromCourse('dessert');
        var totalPrice = appetizer.price + main.price + dessert.price;
        return `your meal is ${appetizer.name}, ${main.name} and ${dessert.name}, The total price is $${totalPrice}`

    }
};

// Now that we’ve finished our menu, object, let’s use it to create 
// a menu by adding some appetizers, mains, and desserts with the 
// .addDishToCourse() function. Add at least 3 dishes to each course (or more if you like!).
menu.addDishToCourse('appetizers', 'Ceasar Salat', 4.7);
console.log

project can be found here (for myself and other helping to access the project in the lesson more easily)

i added a log statement to see some values:

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

as you can see, this._courses contains appetizers, mains and desserts, but they are all undefined.

the .push() method exists for arrays, but it would throw an error when trying to call this method on undefined:

[].push('test'); // works
undefined.push('test'); // throws error

methods like push are data type specific.

i think the getters and setters massively complicate this project, while adding little value.

be careful with giving properties and there getter the same name, this can give recursion:

const myObj = {
  test: 'hello world', // test property
  get test(){
    // getter method for test property
    return this.test;
  }
}
console.log(myObj.test)

at the last line, i call the getter, which should then return the test property which contains the hello world string, however, the getter calls itself, over and over again. Which is why we use underscore:

const myObj = {
  _test: 'hello world',
  get test(){
    return this._test;
  }
}
console.log(myObj.test)

this is why we prefix the property with an underscore (naming convention), now at the last line i call the getter which returns the property value.

as for this project, they do the following:

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  get appetizers() {
    return this._courses.appetizers;
  },
  set appetizers(appetizersIn) {
    this._courses.appetizers = appetizersIn;
  },
}

which means we can do:

console.log(menu.appetizers);

this will call the get method (which is defined in menu object), which in its turn will access appetizers property of _courses object.

2 Likes

Thank you:)
But i still don’t understand how to fix my bug…

that is up to you. You need to decide what to do with your getters and setters, how you want to implement them. My answer was intended as aid to help you better understand the decision you need to make

personally, i would get rid of the getters and setters altogether first:

let menu = {
    courses: {
        appetizers: [],
        mains: [],
        desserts: [],
    },
   // rest of the menu object 
}

and then implement getters and setters slowly if you think you need them.

that is the tricky bit with these projects, people can tell you what they would implement, but learning to implement your own is what you need to learn, and others can only guide you to help achieve this goal, but you are the one that has to achieve it

2 Likes

I did notice that your getters and setters are placed inside your _courses object, which is incorrect.

You need to close that object before your getters and setters appear.