Codecademy meal-maker project

Hello! Here is my completed project for the meal-maker task in the objects pack, this was the most confusing and difficult project I had, I kept having syntax erros and countless attempts to debug it, i finally completed it and have it run smoothly, please take a look and let me know what you think. Also, is there anywhere else I can practice projects like these? Thanks.

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 appetizer = this.getRandomDishFromCourse('appetizers');
    const main = this.getRandomDishFromCourse('mains');
    const dessert = this.getRandomDishFromCourse('desserts');
    const totalPrice = appetizer.price + main.price + dessert.price;
    
    return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name} the price is $${totalPrice}.`;
  }
};

menu.addDishToCourse('appetizers', 'Mozzarella sticks', 5.65);
menu.addDishToCourse('appetizers', 'Chips and Salsa', 4.00);
menu.addDishToCourse('appetizers', 'wings', 8.50);
menu.addDishToCourse('mains','Steak', 20.13);
menu.addDishToCourse('mains','Quesadilla', 12.90);
menu.addDishToCourse('mains','Cheeseburger', 10.90);
menu.addDishToCourse('desserts','cake',6.20);
menu.addDishToCourse('desserts','brownies',6.50);
menu.addDishToCourse('desserts','ice cream',6.90);

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







Giving feedback for the first time? AWESOME! Check out this video for a boost of confidence: How to Review Someone Else’s Code
Feel free to remove this message before posting.

Cheers!
—Codecademy Community Managers

1 Like

Here’s mine. Can you tell me what’s wrong with it? Thanks.

Hi @gguirao13
_courses is supposed to be an object. Check the correct syntax of objects and compare it to your _courses object.

Sorry, but I’m still not seeing it. I have a very difficult time seeing minor details because I’m very nearsighted… Are you referring to the second line?

You’re using parenthesis instead of braces for the object.

Which lines? Do you mean 2 and 6? Those are curly bracers.

These are parenthesis…

Can anyone tell me what’s wrong with mine? I’ve spent ages trying to debug although it keeps giving me an error with my .push method
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 appetizer = this.getRandomDishFromCourse('appetizers');

const main = this.getRandomDishFromCourse('mains');

const dessert = this.getRandomDishFromCourse('desserts');

const totalPrice = appetizer.price + main.price + dessert.price;



return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name} the price is $${totalPrice}.`;

}

};

menu.addDishToCourse(‘appertizers’, ‘Ceasar Salad’, 4.25);

menu.addDishToCourse(‘appertizers’, ‘Garlic Bread’, 4);

menu.addDishToCourse(‘appertizers’, ‘Olives’, 6)

menu.addDishToCourse(‘mains’, ‘steak’, 40);

menu.addDishToCourse(‘mains’, ‘Chicken and Bacon Fetticini’, 28);

menu.addDishToCourse(‘mains’, ‘Spagghetti Bolognese’, 32);

menu.addDishToCourse(‘desserts’, ‘Ice Cream’, 10);

menu.addDishToCourse(‘desserts’, ‘Affogato’, 4);

menu.addDishToCourse(‘desserts’, ‘Cheesecake’, 13);

let meal = menu.generateRandomMeal();

console.log(meal)

Hi @ruby2350228250
Welcome to the forum!
You have better chances of getting help if you post your question in the Get Help category and format your post according to this guideline.
There are already several posts with the same problem. If you don’t have other issues, too, the error message you get is
'TypeError: Cannot read property ‘push’ of undefined at Object.addDishToCourse'
right?
Check if the argument the parameter courseName receives in the method addDishToCourse is the same as the keys in your _courses object.

const menu = {

    _courses: {

        appetizers: [],

        mains: [],

        desserts: [],

    },

    get appetizers() {

        return this._courses.appetizers;

    },

    set appetizers(appetizer) {

        this._courses.appetizers.push(appetizer);

    },

    get mains() {

        return this._courses.mains;

    },

    set mains(main) {

        this._courses.mains.push(main);

    },

    get desserts() {

        return this._courses.desserts;

    },

    set desserts(dessert) {

        this._courses.desserts.push(dessert);

    },

    get courses() {

        return {

            appetizers: this.appetizers,

            mains: this.mains,

            desserts: this.desserts,

        }

    },

    addDishToCourse (courseName, dishName, dishPrice) {

        const dish = {

            dish: dishName,

            price: dishPrice

        };

        this[courseName] = dish;

       

    },

    getRandomDishFromCourse(courseName) {

        const dishes = this._courses[courseName];

        const randomDish = Math.floor(Math.random() * dishes.length);

        return dishes[randomDish];

    },

    generateRandomMeal() {

        const appetizer = this.getRandomDishFromCourse('appetizers');

        const main = this.getRandomDishFromCourse('mains');

        const dessert = this.getRandomDishFromCourse('desserts');

        const totalPrice = appetizer['price'] + dessert['price'] + main['price'];

        return `Your meal is ${appetizer['name']}, ${main.name} and ${dessert.name}. The price is: ${totalPrice}$`;

    }

};

menu.addDishToCourse('appetizers', 'shanghai', 100);

menu.addDishToCourse('appetizers', 'sisig', 180);

menu.addDishToCourse('appetizers', 'calamares', 150);

menu.addDishToCourse('mains', 'adobo', 220);

menu.addDishToCourse('mains', 'sinigang', 240);

menu.addDishToCourse('mains', 'lechon', 500);

menu.addDishToCourse('desserts', 'halo-halo', 80);

menu.addDishToCourse('desserts', 'leche flan', 100);

menu.addDishToCourse('desserts', 'biko', 50);

let meal = menu.generateRandomMeal();

console.log(meal);

Help, why does is outputs to undefined:
Your meal is undefined, undefined and undefined. The price is: 370$

First off, there is a courses getter, so use it.

const dishes = this.courses[courseName];

There is no name property; it’s dish.

    generateRandomMeal() {
        const appetizer = this.getRandomDishFromCourse('appetizers');
        const main = this.getRandomDishFromCourse('mains');
        const dessert = this.getRandomDishFromCourse('desserts');
        const totalPrice = appetizer.price + dessert.price + main.price;
        return `Your meal is ${appetizer.dish}, ${main.dish} and ${dessert.dish}. The price is: ${totalPrice}$`;
    }

This is nothing one wouldn’t have sussed out, eventually. Lesson here is to log your incidental values to see if you are working with the data you expect. That’s how I solved this.

This is where the dish property is defined:

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

If you intended it to be name, then that is where to start revising. Then revise the above solution.


One thing I have to say, it is so nice to see the full menu of setters at work. There are so few posts that go to this extent.

       this[courseName] = dish;
1 Like

thank you so much, this has helped me a lot

1 Like