Meal Maker -- meals not saving to arrays

So I’m working on the Meal Maker project, and I’m not receiving any errors in my code: however, I keep getting this as a result:
image

The numbers do change whenever I refresh the screen, so I know that the objects are being accessed and that they do change, but the name of each meal isn’t being displayed for some reason. I logged the courses to the console and I got the arrays, but rather than listing the meals they simply say [Object object…] for each meal I put in the array (would attach an image but codecademy says I can only attach one image :frowning:)

I am thinking that the error is in the addDishToCourse method, but after watching the video I can’t see any real differences between the demo and my code, so if anyone could help me out I’d greatly appreciate it!

Here’s the code for the section I think is causing the issue, but let me know if you’d like to see another section: thanks again!

You should be saving the arrays correctly…

Double check your generateRandomMeal method. You are returning the dish objects instead of the dish names (property).

1 Like

that worked! thank you!

1 Like

By chance, what ended up being the solution? I seem to be running into the same issue but can’t quite figure it out per the advice provided here!

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

TypeError: Cannot read property ‘length’ of undefined

HELP DEBUGG

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(‘appetizer’);

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}, and ${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’, 5.00);

menu.addDishToCourse(‘desserts’, ‘tofu’, 3.25);

const meal = menu.generateRandomMeal();

console.log(meal);

anyone tell me what’s wrong with this code?
ive combed through it and cant seem to find the issue?

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,
},
this._courses[courseName].push(dish);
},
getRandomDishFromCourse(courseName) {
const dishes = this._courses[courseName];
const randomIndex = Math.floor(Math.random() * dishes.length);
}
generateRandomMeal: {
const appetizer = this.getRandomDishFromCourse(‘appetizers’);
const mains = this.getRandomDishFromCourse(‘mains’);
const desserts = this.getRandomDishFromCourse(‘desserts’); const totalPrice = appetizer.price + mains.price + desserts.price;
return Your meal is ${appetizer.name}, ${mains.name}, and ${desserts.name} and the price is $${totalPrice},;
},
};

menu.addDishToCourse(‘appetizers’, ‘Prawn Cocktail’, 5.50);
menu.addDishToCourse(‘appetizers’, ‘Salad’, 7.00);
menu.addDishToCourse(‘appetizers’, ‘Wings’, 7.50);

menu.addDishToCourse(‘mains’, ‘Steak’, 14.00);
menu.addDishToCourse(‘mains’, ‘Burger’, 8.00);
menu.addDishToCourse(‘mains’, ‘Pasta’, 6.75);

menu.addDishToCourse(‘desserts’, ‘Cheese cake’, 4.00);
menu.addDishToCourse(‘desserts’, ‘Ice Cream’, 2.00);
menu.addDishToCourse(‘desserts’, ‘Cheese Board’, 5.50);

const meal = menu.generateRandomMeal();

console.log(meal);

Hi @array5952959602
please review the article about formatting code:
https://discuss.codecademy.com/t/how-do-i-format-code-in-my-posts/28351
That will make it easier for everyone – and yourself – to recognize syntax errors. That’s the case here. The console’s log:
Uncaught SyntaxError: Unexpected token '}'
Check if all opening brackets have closing counterparts and check the scopes.

(FYI see comment below but I left this here in case anyone else is searching and has the same issue).

Hello, I am getting the same problem. The output is "Your meal is undefined, undefined, undefined. The price is Nan. I have followed the video and can’t see the problem, would love some help. Thanks!

const menu = {

  _courses: {

    appetizers: [],

    mains: [],

    desserts: [],

  },

    get appetizers() {

      return this._courses.appetizers;

    },

    set appetizers(appetizers) {

      this._courses.appetizers = appetizers

    },

    get mains() {

      return this._courses.mains;

    },

    set mains(mains) {

      this._courses.mains = mains

    },

    get desserts() {

      return this._courses.desserts;

    },

    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: this.dishName,

      price: this.dishPrice

    };

    return menu._courses[courseName].push(dish);

  },

    getRandomDishFromCourse(courseName) {

        const dishes = this._courses[courseName]; 

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

        return dishes[randomIndex];

      },

      generateRandomMeal: function() {

        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', 'Olives', 3.50);

 menu.addDishToCourse('appetizers', 'Garlic Bread', 3.25);

 menu.addDishToCourse('appetizers', 'antipasti', 3.75);

 menu.addDishToCourse('mains', 'Roast Beef', 12.80);

 menu.addDishToCourse('mains', 'Pizza', 10.25);

 menu.addDishToCourse('mains', 'Spaghetti Bolognese', 11.50);

 menu.addDishToCourse('desserts', 'Ice Cream', 5.40);

 menu.addDishToCourse('desserts', 'Chocolate Cake', 5.25);

 menu.addDishToCourse('desserts', 'Apple Pie', 5.75);

 console.log(menu.generateRandomMeal());

It’s ok I figured it out.

If anyone else it reading this, it’s because I had put this.dishName within the addDishToCourse method. It should have just said name: dishName.

This is driving me crazy. I keep getting an error saying that it can’t read the property ‘price’ in my array, but when I print out the array as is, it shows the prices and names exactly how it should, with the correct property names attached.

I tested the code also with changing the value of my totalPrice, and it has the same error except for it can’t read the ‘name’ property in my arrays. something is making it so it’s not reading the names of my properties within my array and it’s bugging me. Help would be appreciated.

hi guys, I hope you are well.
I have a problem with my code, but i don’t know what is the problem.
this is my code…
onst 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(appetizer) {

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 + mains.price + dessert.price;

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

}

};

menu.addDishToCourse(‘appetizers’, ‘rabas’, 5.00);

menu.addDishToCourse(‘appetizers’, ‘picada’, 6.00);

menu.addDishToCourse(‘appetizers’, ‘empanadas’, 8.00);

enu.addDishToCourse(‘mains’, ‘pastas’, 9.00);

menu.addDishToCourse(‘mains’, ‘carnes’, 12.00);

menu.addDishToCourse(‘mains’, ‘pescado’, 14.00);

enu.addDishToCourse(‘desserts’, ‘helado’, 5.00);

menu.addDishToCourse(‘desserts’, ‘caffe’, 6.00);

menu.addDishToCourse(‘desserts’, ‘tortas’, 8.00);

const meal = menu.generateRandomMeal();

console.log(meal);
and this is the error!!
I’m sorry for my English.
thanks!

I have the exact code and the exact problem. Did you ever figure it out?

I found some typos (spelling errors) in the code:

in this function:

set appetizers(appetizer) {
   this._courses.appetizers = appetizers;
},

I think that the last appetizers should be appetizer so that it matches the parameter of the function.

in the function getRandomDishFromCourse
in the line

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

mains should be main (because that’s what name is used for the variable a few lines before this line)

A post was split to a new topic: Meal maker

A post was merged into an existing topic: Meal maker