Meal Maker debug

Hello,

type or paste code here

I’m at the tail end of doing the meal maker project for the objects portion of the JavaScript lesson, and I’ve hit a wall. I’m not sure what exactly isn’t working, however I’m getting the following error whenever I try to run the code:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:50
    const totalPrice = appetizers.price + mains.price + desserts.price;

I’m not exactly sure what’s causing this. I’ve looked the error up and tried exploring a few solutions but I’ve come up dry. Any help would be incredibly appreciated.

My code below:

const menu = {

  _courses: {

    appetizers: [],

    mains: [],

    desserts: [],

  },

  get appetizers() {

      },

  set appetizers(appetizerIn){

  },

  get mains(){

  },

  set mains(mainsIn){

  },

  get desserts(){

  },

  set desserts(dessertIn){

  },

  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: function (courseName){

    const dishes = this._courses[courseName];

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

      console.log (dishes + random);

             

  },

  generateRandomMeal(){

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

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

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

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

    return `Your meal is ${appetizers.name}, ${mains.name}, ${desserts.name}, the price is $${totalPrice}.`;

  }

};

menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25);

menu.addDishToCourse('appetizers', 'Big Salad', 3.30);

menu.addDishToCourse('appetizers', 'Minestrone', 5.00);

menu.addDishToCourse('mains', 'Steak', 20.00);

menu.addDishToCourse('mains', 'Stew', 18.00);

menu.addDishToCourse('mains', 'Chicken', 15.00);

menu.addDishToCourse('desserts', 'pie', 5.00);

menu.addDishToCourse('desserts', 'cake', 5.00);

menu.addDishToCourse('desserts', 'ice cream', 5.00);

let meal = menu.generateRandomMeal();

console.log(meal);

Alright! I went through your code line by line in cross-reference to how I solved this and got it to run. Review your getRandomDishFromCourse function on line 65 and see if you possibly misnamed something important there :wink: Think of how you’re also trying to access that variable too, are you properly returning it so the program can use that value? Remember how we access indexes when we’re trying to access a value in a list / dictionary

I figured out the return statement about a minute before you posted, haha.

Can you elaborate on what you mean by the error present in line 65? I ran the code with the return statement and it functioned as I would expect it to. I changed that block of code for consistency sake and it looks like this now:

  getRandomDishFromCourse(courseName){

    const dishes = this._courses[courseName];

      const randomIndex = Math.floor(Math.random() * dishes.length);
      return dishes[randomIndex]},

Ahh you fixed the misnomer! Just make sure you end your return statement with a ;

EDIT: Just to additionally clarify this is what I caught

 const randomIndex = Math.floor(Math.random() * dishes.length)
console.log (dishes + random); 

You were trying to refer to randomIndex as just random and adding the value to dishes, making prices unable to be fetched

Hey Christian! How are you doing?

In the method get courses() you’ve a problem:

  get courses() {
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts
    };

  },

What this is doing is returning an object like this:

{
  appetizers: undefined,
  mains: undefined,
  desserts: undefined,
}

This is because inside get courses() the values this.appetizers, this.mains and this.desserts refer to the getters methods of the same name, which however are empty and so return undefined. However it seems you never need it, so it just here doing nothing.

Also in this line:

    console.log (dishes + random);

random was never defined. You defined randomIndex:

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

But also, if dishes is an array and random just a number, what happens when you add a number to an array in that way? Are you sure this is what you expect?

Now, in the function generateRandomMeal() you call three times getRandomDishFromCourse()passing a different parameter and assigning it to a different constant variable each time. In this logic you expect that getRandomDishFromCourse() return something different each time that you can store in your variables.

However the function getRandomDishFromCourse() doesn’t have a return statement so will return undefined each time.

This is your code corrected, find the differences:

const menu = {

  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },

  get appetizers() {},

  set appetizers(appetizerIn){},

  get mains(){},

  set mains(mainsIn){},

  get desserts(){},

  set desserts(dessertIn){},

  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: function (courseName){
    const dishes = this._courses[courseName];
    const randomIndex = Math.floor(Math.random() * dishes.length)
    return dishes[randomIndex]
  },

  generateRandomMeal(){
    const appetizers = this.getRandomDishFromCourse('appetizers');
    const mains = this.getRandomDishFromCourse('mains');
    const desserts = this.getRandomDishFromCourse('desserts');
    const totalPrice = appetizers.price + mains.price + desserts.price;
    return `Your meal is ${appetizers.name}, ${mains.name}, ${desserts.name}, the price is $${totalPrice}.`;
  }
};

Hope that this helps :slight_smile:

You guys are fantastic, thanks kings!

1 Like