Having trouble with Meal maker

Hi I’ve been having trouble with the meal maker project struggling to understand the steps, I watched the video and noted down what the step was asking for each time, and did the code alongside the professional after doing it along side him my console is printing undefined for the meal variable, any ideas?

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: dishName,

      price: dishPrice,

    };

    return this._courses[courseName].push(dish)

  },

  getRandomDishFromCourse(courseName) {

    const dishes = this._courses[courseName]

    return Math.floor(Math.random() * dishes.length)

    },

  generateRandomMeal() {

    const appetizers = this.getRandomDishFromCourse('appetizers')

    const mains = this.getRandomDishFromCourse('mains')

    const desserts = this.getRandomDishFromCourse('desserts')

    const totalCost = appetizers.price + mains.price + desserts.price;

    return `Your meal is ${appetizers.name}, ${mains.name} and ${desserts.name} and the total price is ${totalCost}`

  }

};

menu.addDishtoCourse('appetizers', 'bread and butter', 5.00)

menu.addDishtoCourse('mains', 'fish and chips', 9.75)

menu.addDishtoCourse('desserts', 'sticky toffee pudding', 7.50)

const meal = menu.generateRandomMeal();

console.log(meal)

my code is there

we can add some extra logs to debug our code:

  getRandomDishFromCourse(courseName) {

    const dishes = this._courses[courseName]

    console.log(Math.floor(Math.random() * dishes.length), dishes.length);
    return Math.floor(Math.random() * dishes.length)

    },

  generateRandomMeal() {

    const appetizers = this.getRandomDishFromCourse('appetizers')

    const mains = this.getRandomDishFromCourse('mains')

    const desserts = this.getRandomDishFromCourse('desserts')
    
    console.log(appetizers);

    const totalCost = appetizers.price + mains.price + desserts.price;

    return `Your meal is ${appetizers.name}, ${mains.name} and ${desserts.name} and the total price is ${totalCost}`

  }

as you can see, your getRandomDishFromCourse does not return a random dish. It just return 0. you need to use the random number you generated to get a random dish. Seems you missed/forgot that step

Thank you I feel extremely dopey now :rofl:

Don’t be, learning to debug code or analyze the flow of your code is skill which takes time to learn

I have being programming for a while, a long while :stuck_out_tongue: it will get better :slight_smile:

I’ll keep practising. Thanks again :slight_smile:

1 Like