Meal Maker ... getRandomDishFromCourse is not a function

Hello, i have this error :
" maker/app.js:23
const appetizers = this.courses.getRandomDishFromCourse(‘appetizers’);
^

TypeError: this.courses.getRandomDishFromCourse is not a function
at Object.generateRandomMeal … "

I think there is an error with “this” probably but I do not see what!

My code :

const menu = {
    courses : {
      appetizers: [],
      mains: [],
      desserts: []
    },

    addDishToCourse(courseName, dishName, dishPrice ) {
      const dish = {
        name: dishName,
        price: dishPrice,
      };
      return this.courses[courseName].push(dish);
    },
  
    getRandomDishFromCourse(courseName) {
      const dish = this.courses[coursesName];
      const randomDish = Math.floor(Math.random()* dish.length);
      return dish[randomDish];
    },
  
    generateRandomMeal () {
      const appetizers = this.courses.getRandomDishFromCourse('appetizers');
      const mains = this.courses.getRandomDishFromCourse('mains');
      const desserts = this.courses.getRandomDishFromCourse('desserts');
      const price = appetizers.price + mains.price + desserts.price;
      return ` Le menu de ce soir se composera de : ${appetizers}, ${mains}, ${desserts}, le prix de ce repas est de : ${price} €. Bon appétit. `;
    },
  };
  
  
  menu.addDishToCourse('appetizers', 'Champagne', 30);
  menu.addDishToCourse('appetizers', 'Biere', 5);
  menu.addDishToCourse('appetizers', 'Cacahuete', 2);
  
  menu.addDishToCourse('mains', 'Omelette', 5);
  menu.addDishToCourse('mains', 'spagetti', 10);
  menu.addDishToCourse('mains', 'Salade', 8);
  
  menu.addDishToCourse('desserts', 'Creme brulée', 4);
  menu.addDishToCourse('desserts', 'Fromage', 10);
  menu.addDishToCourse('desserts', 'fondant', 5);
  
  const meal = menu.generateRandomMeal();
  console.log(meal);
  //console.log(menu._courses);

Hello, and welcome to the forums!

You’re really close. It’s because you’re trying to call getRandomDishFromCourse() as if it is inside the courses object, but only your arrays for the dishes exist in there.

Click for more details

getRandomDishFromCourse() is defined at the top-level of the object, so you only need this.getRandomDishFromCourse() when calling it from the same level.

Example:

const mains = this.getRandomDishFromCourse('mains');

HAHA :sweat_smile: thx !

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.