Meal Maker ... getRandomDishFromCourse is not a function

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

TypeError: 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,
    getRandomDishFromCourse(courseName) {
      const dish =[coursesName];
      const randomDish = Math.floor(Math.random()* dish.length);
      return dish[randomDish];
    generateRandomMeal () {
      const appetizers ='appetizers');
      const mains ='mains');
      const desserts ='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();

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.


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.