Meal Maker

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.appetizers = 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('appetizer');
     const main = this.getRandomDishFromCourse('main');
     const dessert = this.getRandomDishFromCourse('dessert');
    const totalPrice = appetizer.price + main.price + dessert.price;
    return `The items you are going to eat tonight are ${appetizer.name}, ${main.name}, and +${dessert.name}.  The total price of your meal tonight is ${totalPrice}.`;
  }
};

menu.addDishToCourse('appetizer', 'soup', 3.00);
menu.addDishToCourse('appetizer', 'salad', 4.00);
menu.addDishToCourse('appetizer', 'wings', 5.00);

menu.addDishToCourse('main', 'steak', 23.00);
menu.addDishToCourse('main', 'salmon', 20.00);
menu.addDishToCourse('main', 'grilled chicken', 15.00);

menu.addDishToCourse('dessert', 'cake', 7.00);
menu.addDishToCourse('dessert', 'pie', 6.00);
menu.addDishToCourse('dessert', 'cookie', 4.00);

const meal = menu.generateRandomMeal();

console.log(meal)

I am getting an error with my push dish to Dishname

This is my error on the console.

1 Like

I know what your error is but it is better if you find out yourself ;). You may want to add a console.log here and there to find out what is going on. And draw conclusions from there :wink:

1 Like

We should turn that into a snappy motto… like “If in doubt, print it out!”. :joy:

(Print it to the console, obviously… let’s not waste paper!)

4 Likes

Thats actually an awesome Codecademy T Shirt idea! @alyssavigil

2 Likes

…what have I started!? :sweat_smile:

There’ll be a line of mugs and coasters next…

2 Likes

Lol, but lets get back on topic, @esaldivar the stage is yours again :wink:

1 Like

So researching the error the push cannot read courseName and push the values (maybe thats the wrong word) into the array for this object. Somehow it is reading the courseName as undefined and I need to define it to push the objects I want into the array.

Sorry if this explanation is hard to understand. I am 2 weeks into coding and this project has been a huge challenge for me. Thanks for all the help and support in advance.

2 Likes

you need to trace where the undefined comes from. I would recommend starting with logging (using console.log() ) to identify the problem

1 Like

You are quite on track! Now what is wrong with courseName?

It seems that I didn’t set dish as a variable

addDishToCourse(courseName, dishName, dishPrice) {
dish = {
      name: dishName,
      price: dishPrice
    };
  // testing dish //
     console.log(dish)    
  // end of test with dish //
    return this._courses[courseName].push(dish);
}

I changed the code to define dish as a variable by putting const in front of dish

addDishToCourse(courseName, dishName, dishPrice) {

const dish = {
      name: dishName,
      price: dishPrice
    };
  // testing dish //
     console.log(dish)    
  // end of test with dish //
    return this._courses[courseName].push(dish);
}

I still get the same error though. I know I have to define courseName and my thought is that this part of my code is not recognizing courseName as an array of objects.

1 Like

courseName is a variable holding the course of the menu which your dish will be added to. It is not an array at all. It is assigned based on the value you pass it when you call the addDishToCourse function.

So, for example, let’s take your first call to that function:
menu.addDishToCourse('appetizer', 'soup', 3.00);

What is the value of courseName going to be here? At this point, you ought to be able to see your error. :slight_smile:

1 Like

js object error_menu

The console log only generated an appetizer. I am thinking that my _courses is only picking up an appetizer key but I am unsure. I have restarted this project 3-4 times and the failure is helping me identify what is going wrong but I am still missing the why and where.

1 Like

The error is telling you exactly what’s wrong, I think the issue is you don’t understand what it’s saying.

Your error is:

TypeError: Cannot read property 'push' of undefined
    at Object.addDishToCourse

This error is basically saying “I cannot call ‘push’ on an object which is undefined”.

To refer back to my previous post:

Can you answer that question? (Hint: You can work it out quite easily with an appropriately placed call to console.log().)

The answer to that question will tell you which course JavaScript is attempting to access. Why, perhaps, might this result in undefined rather than what you’re expecting to happen?

1 Like

@esaldivar you are soo close!!

You’ll smack yourself in the head the moment you find it :wink:

Yes, @esaldivar most likely will - but also they shouldn’t be discouraged by this! We’ve all been in this position at one point or another, unable to see the wood for the trees… (I can’t think of a similar idiom that involves a rubber duck…)

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);
  },
    // inserted code from myself //
  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 `The items you are going to eat tonight are ${appetizer.name}, ${main.name}, and ${dessert.name}.  The total price of your meal tonight is ${totalPrice}.`;
  }
};

// generated meals below //

menu.addDishToCourse('appetizers', 'soup', 3.00);
menu.addDishToCourse('appetizers', 'salad', 4.00);
menu.addDishToCourse('appetizers', 'wings', 5.00);

menu.addDishToCourse('mains', 'steak', 23.00);
menu.addDishToCourse('mains', 'salmon', 20.00);
menu.addDishToCourse('mains', 'grilled chicken', 15.00);

menu.addDishToCourse('desserts', 'cake', 7.00);
menu.addDishToCourse('desserts', 'pie', 6.00);
menu.addDishToCourse('desserts', 'cookie', 4.00);

// end of generated meals //

const meal = menu.generateRandomMeal();

console.log(meal)

CA_JSProject_consoleFinalResult

2 Likes

Naming convention issue. Did not have s on strings to connect to arrays. Thank you so much for your help.

1 Like

We knew you’d spot it eventually. :slight_smile:

Pop back to the forum if there’s anything else we can help with. :+1:

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