Meal maker

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 error is this;
const randomIndex = Math.floor(Math.random() * dishes.length);

You think I am capable of telling you what your problem is from that single line of code? I feel flattered you hold much in such high regard. However, that is not the case

Please include your full code.

1 Like

Okay, this is my full code ;

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  
  get appetizers () {
    return this._courses.appetizers;
  },

  set appetizers (appetizersIn) {
    this._courses.appetizers = appetizersIn;
  },
  
  get mains () {
    return this._courses.mains;
  },

  set mains (mainsIn) {
    this._courses.mains = mainsIn;
  },
  
  get desserts () {
    return this._courses.desserts;
  },

  set desserts (dessertsIn) {
    this._courses.deserts = dessertsIn;
  },

  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); 
    console.log(dishes, courseName)
    return dishes[randomIndex];
  },

  generateRandomMeal() {
    const appetizer = this.getRandomDishFromCourse('appetizers');
    const main = this.getRandomDishFromCourse('mains');
    const dessert = this.getRandomDishFromCourse('deserts');
    const totalPrice = appetizer.price + main.price + dessert.price;
    return `The name of your dish is ${appetizer.name}, ${main.name}, ${dessert.name} and your price is ${totalPrice}`;
  }
}

menu.addDishToCourse('appetizers', 'Small chops', 5.56);
menu.addDishToCourse('appetizers', 'Wings', 3.45);
menu.addDishToCourse('appetizers', 'Fries', 5.46);

menu.addDishToCourse('mains', 'Salmond', 10.34);
menu.addDishToCourse('mains', 'Chicken', 7.34);
menu.addDishToCourse('mains', 'Tacos', 11.23);

menu.addDishToCourse('desserts', 'Ice Cream', 4.24);
menu.addDishToCourse('desserts', 'Cake', 3.37);
menu.addDishToCourse('desserts', 'Sausage', 5.43);

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

using log like you do here:

console.log(dishes, courseName)

you could use a console.log() before the error line to see which courseName doesn’t exists?

there are only 3 possibilities:

    const appetizer = this.getRandomDishFromCourse('appetizers');
    const main = this.getRandomDishFromCourse('mains');
    const dessert = this.getRandomDishFromCourse('deserts');

which of these keys: appetizers, mains or deserts doesn’t exists as key/property in _courses object?

1 Like

Its says desserts is undefined. How can i go about it please ?

desserts is valid key for _courses object, deserts isn’t

1 Like

Yes it is, but it doesn’t print desserts to the console

No, it isn’t… and at this point, your error ought to be obvious. :slight_smile:

console.log("desserts" === "deserts");
//output: false

Sorry I’m kinda confused but the thing here is…Im tryna log to the console but its saying "The program says that it is not able to read ‘length’ property of ‘dishes’ in ‘getRandomDishFromCourse’ "

can someone just tell me what the issue is

You try to access a property on an object which doesn’t exists. Which results in undefined. (in my opinion, this is where Javascript throw an error). Now your program crashes at a later point, which is confusing

so like we have been trying to tell you, you need to fix this:

const dessert = this.getRandomDishFromCourse('deserts');

desert (the argument you pass the the method/function) doesn’t exists on the courses object here:

const dishes = this._courses[courseName];
2 Likes

Oh Oh Oh yeah, thank you and i really appreciate, just a single letter mistake that causes the program not to run lol. Have a good day​:blush::heart:

1 Like