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

Regarding step 10
In the body of the getter, create an if…else statement to check if _meal and _price values exist (or are truthy values). If so, return a string telling potential website visitors what Today’s Special is. For example: “Today’s Special is Spaghetti for $5!”

get todaysSpecial() {
   if ( this._meal && this._price )
    {return 'Today’s Special is Spaghetti for $5!'
    } else { return 'Meal or price was not set correctly!'}
  }

It’s asking to check if meal & price properties have truthy values; why do we only need to type if ( this._meal && this._price )as opposed to if ( this._meal === true && this._price === true ) since it’s asking me to check if they have truthy values? Thank you in advance!

if(this._meal && this.price) only checks whether both values are truthy
(which includes being non-empty strings, non-zero numbers)
And if one of them is undefined, this._meal && this.price would be falsy

truthy means it acts like true for the purpose of the if, not that it must have the value true
falsy means it acts like false for the purpose of the if, not that it must have the value false

in if( this._meal === true && this._price === true )
this._meal === true
checks if this._meal is exactly the value true
if this._meal is a a string, then this._meal === true would be false
and thus the whole thing would be false

1 Like

Hi. Joe McKenna from Iowa City here. For the last couple of days(Jun 2023) I’ve been stuck on the meal maker project.
My sticking point is step 9, the one of get todaysSpecial function. I’m getting messages that it’s unidentified and not getting past that. What’s a mother to do? Thanks in advance for any help with this.

Please post a link to the exercise so we can see what you see and perhaps even see our own code from years past. It will speed up the process of getting you back underway.

Don’t be shy, post your code for us to examine and criticize, as needs be. That’s another way to get to a resolve and make progress.