Meal maker

i was looking for that info. Thanks!

1 Like

Hi Dear all,

I need some help… I wonder if I was blind or something… I kept getting errors like: property “push” of undefined … I tried multiple times on my own to adjust it. Then, I read the spoiler provided by Roy @mtf .

Then I tried to adjust my code after looking at Roy’s. I really think at this point my code is literally identical as Roy’s code … but if I copy and paste Roy’s code, it works, but mine just still doesn’t work. I cannot see where is the problem. Can I please have a fresh pair of eyes to help me out? Thank you so much.

I think this part is where the problem is:

Roy’s code:
const menu = {
_courses: {
_appetizers: ,
_mains: ,
_desserts: ,

get appetizers(){
  return this._appetizers;
},
set appetizers(appetizerIn){
  this._appetizers.push(appetizerIn);
},
get mains(){
  return this._mains;
},
set mains(mainIn){
 this._mains.push(mainIn);
},
get desserts(){
  return this._desserts;
},
set desserts(dessertIn){
  this._desserts.push(dessertIn);
},

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

My code:
const menu = {
_courses : {
appetizers: ,
mains: ,
desserts: ,

get appetizers () {
  return this._appetizers;
},
set appetizers(appetizerInfo) {
  this._appetizers.push(appetizerInfo);
},
get mains () {
  return this._mains;
},
set mains (mainsInfo) {
  this._mains.push(mainsInfo);
},
get desserts () {
  return this._desserts;
},
set desserts (dessertsInfo) {
  this._desserts.push(dessertsInfo);
},

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

The rest of the code in case needed:
My Code:
addDishToCourse (courseName, dishName, dishPrice) {
const dish = {
name : dishName,
price: dishPrice,
};
this._courses[courseName].push(dish);
},

getRandomDishFromCourse (courseName) {
var dishes = menu._courses[courseName];
var randomIndex = Math.floor(Math.random() * dishes.length);
return dishes[randomIndex];
},

generateRandomMeal () {
var appetizer = this.getRandomDishFromCourse (‘appetizers’);
var main = this.getRandomDishFromCourse (‘mains’);
var dessert = this.getRandomDishFromCourse (‘desserts’);
const totalPrice = appetizer.price + main.price + dessert.price;

return `You ordered `+`appetizer: `+appetizer.name + `main: ` + main.name + `dessert: ` + dessert.name + `.` + `The total price is ` + totalPrice + `.`;

},
};

menu.addDishToCourse(“appetizers”, “Coctel de camaron”, 140);
menu.addDishToCourse(“appetizers”, “Boquerones”, 80);
menu.addDishToCourse(“appetizers”, “rabas”, 100);

menu.addDishToCourse(“mains”, “Pasta de mar”, 140);
menu.addDishToCourse(“mains”, “Pesca del da”, 180);
menu.addDishToCourse(“mains”, “Sorpresa de mariscos”, 200);

menu.addDishToCourse(“desserts”, “Creme Brul”, 110);
menu.addDishToCourse(“desserts”, “Sopa dulce”, 110);
menu.addDishToCourse(“desserts”, “Manzana asada”, 80);

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

Your code does not use backing variables so remove all the underscores from _appetizers, _mains, and _desserts.

Hi Guys,

I have managed to almost complete this task and debug a few simple errors I have made but this last error has me stumped:

Any Ideas on where I am going wrong? I have been staring at this code for what seems like forever and I cannot see this issue!

Many thanks in advance!!

It will likely trace back to where the attributes are created and defined. We cannot see enough of your code to determine exactly where. Please post the actual code in a reply.

Hi,
I wasn’t able to formulate to problem from step 7, basically I would never be able to wright the whole thing with my current knowledge… I need to improve and practice. To complete I had to follow the video unfortunately… :frowning:

Never the less, I added 2 bits from my own to make the project better.

the first is to add decimals:

const totalPrice = (appetizer.price + main.price + dessert.price).toFixed(2);

the second is to add a dollar sign, actually it took few attemps to get it right:

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

exactly one year later I had the same issue, THANK YOU! I cannot get myself used to the setter syntax, need to drill myself on that.

found a separate thread where you clarify this -Meal Maker: Adding code inside the menu item Getters & Setters

Actually, when I use this, it seems to assign instead of push. I tested by logging after adding two appetizer dishes and it only contains the last dish added:

const menu = {
_courses: {
appetizers: ,
mains: ,
desserts:
},
get appetizers() {
return this._courses.appetizers;
},
set appetizers(app) {
this._courses.appetizers.push(app);
},
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 = {
name: dishName,
price: dishPrice,
};
this._courses[courseName] = dish;
},
getRandomDishFromCourse(courseName) {
const dishes = this._courses[courseName];
return dishes[Math.floor(Math.random() * dishes.length)];
},
getRandomMeal() {
const appetizer = getRandomDishFromCourse(‘appetizers’);
const main = getRandomDishFromCourse(‘mains’);
const dessert = getRandomDishFromCourse(‘desserts’);
const totalPrice = appetizer.price + main.price + dessert.price;
return Your meal will include the ${appetizer} appetizer, ${main} main dish & ${dessert} dessert. The total price is $${totalPrice};
}
}

menu.addDishToCourse(‘appetizers’, ‘Hommos’, 4);
menu.addDishToCourse(‘appetizers’, ‘Peas’, 2);
console.log(menu.appetizers);

This outputs only the ‘Peas’ dish…

Please post a link to this exercise, 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(para) {

this._courses.appetizers = para

},

set mains(para) {

this._courses.mains = para

},

set desserts(para) {

this._courses.desserts = para

},

get courses() {

return {

  appetizers: this._courses.appetizers, 

  mains: this._courses.mains,

  deserts: this._courses.desserts,

}

},

addDishToCourse(courseName, dishName, dishPrice) {

const dish = {

  name: dishName, 

  price: dishPrice,

}

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

},

getRandomDishFromCourses(courseName) {

const dishes = this._courses[courseName]

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

return dishes[randomIndex]

},

generateRandomMeal(){

appetizer = this.getRandomDishFromCourses('appetizers')

main = this.getRandomDishFromCourses('main')

dessert = this.getRandomDishFromCourses('dessert')

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

return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name} and it costs ${totalPrice}.`

}

};

menu.addDishToCourse(‘appetizers’, ‘mini burgers’, 5.00)

menu.addDishToCourse(‘appetizers’, ‘bread’, 6.00)

menu.addDishToCourse(‘appetizers’, ‘olives’, 5.50)

menu.addDishToCourse(‘main’, ‘steak’, 22.00)

menu.addDishToCourse(‘main’, ‘chicken’, 13.50)

menu.addDishToCourse(‘main’, ‘fish’, 16.20)

menu.addDishToCourse(‘desserts’, ‘ice cream’, 5.20)

menu.addDishToCourse(‘desserts’, ‘brownies’, 5.40)

menu.addDishToCourse(‘desserts’, ‘plum pudding’, 3.00)

meal = menu.generateRandomMeal()

console.log(meal)

I keep getting an error message saying Cannot read property of push undefined!

Can anyone help? Thanks a million.

Hello, @1tonnepy, and welcome to the forums!

Consider how you arrived at the error. If I had an object, and tried to access a property of that object that doesn’t exist, I would be trying to access something that is undefined. For example:

const someGroup = {
  names: [],
  ages: []  
}

someGroup["names"].push("Alice")
someGroup["ages"].push(42)

console.log(someGroup) // { names: [ 'Alice' ], ages: [ 42 ] }

//so far, so good, but if I try:
someGroup["name"].push("Joe") // TypeError: Cannot read property 'push' of undefined 
//so, what's the problem? Does someGroup have a property named "name"?

This is what is happening in your code. The error message should include information that will help you identify where to look. Don’t forget that you can print out values to see if they match what you are expecting.

Hi @midlindner,

Thank you so much for the reply! It is so helpful!
I understand that in your example there was ‘name’ property but only ‘names.’ I added a courseName property into the object but it still says the same message! I am so sorry I can’t get this right!

Hi! I corrected that problem! I see what you did with the plural and singular as I wrote main not mainS! THANK YOU!

1 Like

Hi guys,

I found this exercise highly confusing at first and decided to break it down.

It’s all clear to me now and I wrote down explanations that helped me understand the code, which I’m sharing below in case it can help someone else, too.

A note: the ‘setters’ in this particular piece of code are useless, since the attributes for the dishes are all defined by the .push method, and the using the setters would replace the entire array that was defined by the .push method once we add a new dish.

Ps. Apologies for the format:

The setters don’t have to be useless. What if we write them with push? Then in the add dish method,

this._courses[courseName] = dish

My code is complete with no errors, i was just wondering what does “return this._courses[courseName].push(dish);” in line 37 actually do? I’m a little confused with it, and also why does “course” need an underscore also in line 37.

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(appetizerIn) {
    this._courses.appetizers = appetizerIn;
  },
  set mains(mainsIn) {
    this._courses.mains = mainsIn;
  },
  set desserts(dessertsIn) {
    this._courses.desserts = dessertsIn;
  },
  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} and the total price is ${totalPrice}`;
  }
};

menu.addDishToCourse('appetizers', 'salad', 4.00);
menu.addDishToCourse('appetizers', 'wings', 4.50);
menu.addDishToCourse('appetizers', 'fries', 5.00);

menu.addDishToCourse('mains', 'steak', 10.25);
menu.addDishToCourse('mains', 'salmon', 7.75);
menu.addDishToCourse('mains', 'tofu', 11.20);

menu.addDishToCourse('desserts', 'ice-cream', 3.00);
menu.addDishToCourse('desserts', 'coffee', 3.00);
menu.addDishToCourse('desserts', 'cake', 3.25);


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


Technically there would be no need to return since none is expected. The action takes place right in the statement.

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

On another technical note, if we were to examine that return value what would it be? None? Or a value, such as the length of the array after the item was pushed onto it? Answer, the latter. .push() returns the new length. However we don’t need that value, and hence don’t need return.

1 Like

The teacher for this lesson just showed how to get the answers and did not explain anything. Anyone have any better resources for these concepts?

9 posts were split to a new topic: Price is undefined