'Meal maker' project

I am stuck on the end of ‘meal maker’ project in ‘objects’ part. The program says that it is not able to read ‘length’ property of ‘dishes’ in ‘getRandomDishFromCourse’. The part of the code, where error is shown, is given below.
const randomIndex = Math.floor(Math.random() * dishes.length);

https://www.codecademy.com/courses/introduction-to-javascript/projects/meal-maker

Please post your full code, the error might originate somewhere else.

1 Like

I posted a link for the code

The link you posted is to the specific exercise. If any of us follow it, we see our own code from the exercise not yours. You need to copy your code, and paste it in a post. Click on the </> icon first, and paste your code in the space indicated. Thanks!

I have copied my code below:

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: courseName,
      price: dishPrice
    }
     this._courses[courseName].push(dish);
  },
getRandomDishFromCourse(courseName) {
    const dishes = menu._courses[courseName];
    const randomIndex = Math.floor(Math.random() * dishes.length);
  return dishes[randomIndex];
 },
 generateRandomMeal() {
   const appetizers = this.getRandomDishFromCourse('appetizers');
   const mains = this.getRandomDishFromCourse('mains');
   const desserts = this.getRandomDishFromCourse('dessert');
   const totalPrice = appetizers.price + mains.price + desserts.price
   return`Your meal is ${appetizers.name}, ${mains.name} and ${desserts.name} The price is ${totalPrice}.`;
   
 }
};
menu.addDishToCourse('appetizers', 'bruschetta', 6);
menu.addDishToCourse('appetizers', 'chicken tikka', 6.75);
menu.addDishToCourse('appetizers', 'momos', 5);
menu.addDishToCourse('mains', 'pasta', 6.75);
menu.addDishToCourse('mains', 'biryani', 7);
menu.addDishToCourse('mains', 'dal-roti', 7.25);
menu.addDishToCourse('desserts', 'icecream', 6.5);
menu.addDishToCourse('desserts', 'pastry', 6.5);
menu.addDishToCourse('desserts', 'rasgulla', 7);

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

thanks!

if we insert a log statement:

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

we can see that calling getRandomDishFromCourse with argument value dessert gives undefined, looking at the properties of courses object we can see that the property is named desserts. Which explains the error

1 Like

Thanks for your help !

Is this how to share my code? Toward end of Meal Maker, i have to create dish object inside menu object. My word dish is blue but in the hint it’s red. Anyway, I can’t understand the hint or what I’m doing with this step. i watched the video a couple of times. Would anyone like to point me in the right direction/ Thank you!!

here:

_courses: {
    appetizers: [],
    mains: [],
    desserts:[]
  };

why a semi-colon? after all the other property value pairs you use comma’s (,) which is correct, so why a semi-colon here?

I guess I’m really not clear yet. the very first lesson told us to get in the habit of ending things with a semi colon.
then I learned that methods within objects should be separated by commas.

Maybe i need to google when to use each; thank you for directing me to the issue!!!

closing statements with a semi-colon is good practice, however, an object is not a statement

In the HINT, the color of the code for addDishToCourse is red,
but in my code, it’s light blue AND i get no error message AND it matches the color on the video help.
It might help me learn this if anyone could explain why both colors would work?

It’s not the colors, but the code that works. Syntax highlighting can vary from one environment to another. Work in a black on white setting for awhile and it will remove all the color from your mind and let you focus on the code.

Regardless the environment you work in after that the color will give off signals our brain can pick up on.

hmmmm. okay thank you vry much!

1 Like

I have been trying to work out what I am doing wrong for hours and here I have an identical issue with an identical solution! Thanks a bunch.

If you need help, please post your code so we can help you :slight_smile:

I got it in the end. I was entering an incorrect string the entire time. Thanks though.

1 Like

Nicely done to get the answer yourself :slight_smile: Hopefully it taught you something about debugging. These kind of struggles make you a better developer :slight_smile: