Meal Maker Help

Hi Guys,

im trying to figure why isnt my code working , but cannot find the error…

Please Help :slight_smile:

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  get appetizers() {
    return this.appetizers;
  },
  set appetizers(appetizerIn) {
    this.appetizers = appetizerIn;
  },
  get mains() {
    return this.mains;
  },
  set mains(mainsIn) {
    this.mains = mainsIn;
  },
  get desserts() {
    return this.desserts;
  },
  set desserts(dessertsIn) {
    this.desserts = dessertsIn;
  },
  // This uses the appetizer getter method
  get courses() {
    return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts,
    };
  },

  //The .addDishToCourse() will be used to add a new dish to the specified course on the menu.
  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    this._courses[courseName].push(dish);
  },

  //This function which will allow us to get a random dish from a course on the menu, which will be necessary for generating a random meal.
  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 mains = this.getRandomDishFromCourse("mains");
    const desserts = this.getRandomDishFromCourse("desserts");
    const calculatePrice = appetizer.price + main.price + dessert.price;
    return `Your Meal consisted on ${apetizer.name}, ${main.name} and ${dessert.name}, total price is  ${CalculatePrice}€ .`;
  },
};

menu.addDishToCourse("appetizers", "Ensaladilla Rusa", 6);
menu.addDishToCourse("main", "Rabo de toro", 12);
menu.addDishToCourse("dessert", "Crema Catalana", 8);

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

1 Like

It has to do with the arguments you are passing into addDishToCourse. There is no “main” nor “dessert” in your menu._courses object. But there is a “mains” and “desserts”.

2 Likes

Thanks a lot .
Ok , so i changed that , now it gives me a different error :slight_smile:

Math.floor(Math.random() * dishes.length);
^
TypeError: Cannot read property ‘length’ of undefined

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  get appetizers() {
    return this.appetizers;
  },
  set appetizers(appetizerIn) {
    this.appetizers = appetizerIn;
  },
  get mains() {
    return this.mains;
  },
  set mains(mainsIn) {
    this.mains = mainsIn;
  },
  get desserts() {
    return this.desserts;
  },
  set desserts(dessertsIn) {
    this.desserts = dessertsIn;
  },
  // This uses the appetizer getter method
  get courses() {
    return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts,
    };
  },

  //The .addDishToCourse() will be used to add a new dish to the specified course on the menu.
  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    this._courses[courseName].push(dish);
  },

  //This function which will allow us to get a random dish from a course on the menu, which will be necessary for generating a random meal.
  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 mains = this.getRandomDishFromCourse("main");
    const desserts = this.getRandomDishFromCourse("dessert");
    const calculatePrice = appetizer.price + main.price + dessert.price;
    return `Your Meal consisted on ${apetizer.name}, ${main.name} and ${dessert.name}, total price is  ${CalculatePrice}€ .`;
  },
};

menu.addDishToCourse("appetizers", "Ensaladilla Rusa", 6);
menu.addDishToCourse("mains", "Rabo de toro", 12);
menu.addDishToCourse("desserts", "Crema Catalana", 8);

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

1 Like
  generateRandomMeal() {
    const appetizer = this.getRandomDishFromCourse("appetizer");
    const mains = this.getRandomDishFromCourse("main");
    const desserts = this.getRandomDishFromCourse("dessert");

Same problem as before. Arguments don’t match what is inside of menu._courses

2 Likes

Thanks ¡¡

ok , so , now i get a different error now .

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

TypeError: Cannot read property ‘length’ of undefined

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  get appetizers() {
    return this.appetizers;
  },
  set appetizers(appetizerIn) {
    this.appetizers = appetizerIn;
  },
  get mains() {
    return this.mains;
  },
  set mains(mainsIn) {
    this.mains = mainsIn;
  },
  get desserts() {
    return this.desserts;
  },
  set desserts(dessertsIn) {
    this.desserts = dessertsIn;
  },
  // This uses the appetizer getter method
  get courses() {
    return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts,
    };
  },

  //The .addDishToCourse() will be used to add a new dish to the specified course on the menu.
  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    this._courses[courseName].push(dish);
  },

  //This function which will allow us to get a random dish from a course on the menu, which will be necessary for generating a random meal.
  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 calculatePrice = appetizer.price + main.price + dessert.price;
    return `Your Meal consisted on ${apetizer.name}, ${main.name} and ${dessert.name}, total price is  ${CalculatePrice}€ .`;
  },
};

menu.addDishToCourse("appetizers", "Ensaladilla Rusa", 6);
menu.addDishToCourse("mains", "Rabo de toro", 12);
menu.addDishToCourse("desserts", "Crema Catalana", 8);

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

Still the same problem.

2 Likes

Ok , thanks ¡¡

now a different error again :

return `Your Meal consisted on ${apetizer.name}, ${main.name} and ${dessert.name}, total price is  ${CalculatePrice}€ .`;
                                 ^

ReferenceError: apetizer is not defined

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  get appetizers() {
    return this.appetizers;
  },
  set appetizers(appetizersIn) {
    this.appetizers = appetizersIn;
  },
  get mains() {
    return this.mains;
  },
  set mains(mainsIn) {
    this.mains = mainsIn;
  },
  get desserts() {
    return this.desserts;
  },
  set desserts(dessertsIn) {
    this.desserts = dessertsIn;
  },
  // This uses the appetizer getter method
  get courses() {
    return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts,
    };
  },

  //The .addDishToCourse() will be used to add a new dish to the specified course on the menu.
  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    this._courses[courseName].push(dish);
  },

  //This function which will allow us to get a random dish from a course on the menu, which will be necessary for generating a random meal.
  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 calculatePrice = appetizer.price + main.price + dessert.price;
    return `Your Meal consisted on ${apetizer.name}, ${main.name} and ${dessert.name}, total price is  ${CalculatePrice}€ .`;
  },
};

menu.addDishToCourse("appetizers", "Ensaladilla Rusa", 6);
menu.addDishToCourse("mains", "Rabo de toro", 12);
menu.addDishToCourse("desserts", "Crema Catalana", 8);

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

ok i just found some Typos and also that the first part the properties should be named differently so will not enter in conflict with other part of the code . Am i right ?

const menu = {
  _courses: {
    _appetizers: [],
    _mains: [],
    _desserts: [],

  get appetizers() {
    return this._appetizers;
  },
  set appetizers(appetizersIn) {
    this._appetizers = appetizersIn;
  },
  get mains() {
    return this._mains;
  },
  set mains(mainsIn) {
    this._mains = mainsIn;
  },
  get desserts() {
    return this._desserts;
  },
  set desserts(dessertsIn) {
    this._desserts = dessertsIn;
  },

   },
  // This uses the appetizer getter method
  get courses() {
    return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts,
    };
  },

  //The .addDishToCourse() will be used to add a new dish to the specified course on the menu.
  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    this._courses[courseName].push(dish);
  },

  //This function which will allow us to get a random dish from a course on the menu, which will be necessary for generating a random meal.
  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 calculatePrice = appetizer.price + main.price + dessert.price;
    return `Your Meal consisted on ${appetizer.name}, ${main.name} and ${dessert.name}, total price is  ${calculatePrice}€ .`;
  },
};

menu.addDishToCourse("appetizers", "Ensaladilla Rusa", 6);
menu.addDishToCourse("mains", "Rabo de toro", 12);
menu.addDishToCourse("desserts", "Crema Catalana", 8);
menu.addDishToCourse("appetizers", "Ensalada de la Casa", 12);
menu.addDishToCourse("mains", "Entrecot", 18);
menu.addDishToCourse("desserts", "Tocino de Cielo", 9);
menu.addDishToCourse("appetizers", "Porra", 6);
menu.addDishToCourse("mains", "Solomillo de Ternera", 12);
menu.addDishToCourse("desserts", "Puding", 6);

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

Best way to check if you’re right and that your code runs would be to run it… no? :slight_smile:

1 Like