Meal project

This is redo 8 and it was from the walk through video and I still get the same errors.

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

keeps giving me a typeof error.

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  get appetizers() {
    return this._courses.appetizers;
  },
  get mains() {
    return this._courses.mains;
  },
  get desserts() {
    return this._desserts;
  },
  set appetizers(appetizers) {
    this._courses.appetizers = appetizers;
  },
  set mains(mains) {
    this._courses.mains = mains;
  },
  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];
    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 totalPrice = appetizer.price + main.price + dessert.price;
    return `Your meal is ${appetizer.name}, ${main.name}, and ${dessert.name}, and the total price is $${totalPrice}.`;
  }
};

menu.addDishToCourse('appetizer', 'Caesar Salad', 4.25);
menu.addDishToCourse('appetizer', 'wings', 5.25);
menu.addDishToCourse('appetizer', 'fries', 1.25);

menu.addDishToCourse('mains', 'chicken', 7.25);menu.addDishToCourse('mains', 'beef', 8.25);menu.addDishToCourse('mains', 'soup', 6.25);

menu.addDishToCourse('desserts', 'ice cream', 3.25);menu.addDishToCourse('desserts', 'cake', 3.75);menu.addDishToCourse('desserts', 'cookies', 1.25);

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

so we need to find out why we get undefined, so i did the following:

addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    console.log(dishName, courseName, this._courses[courseName]);
    return this._courses[courseName].push(dish);
  },

i logged dishName to see which dish we are trying to add, courseName to see to which course we want to add this dish.

so then i looked here:

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

and i understood the error, there is not appetizer course (value of courseName)

besides fixing the error, you could harden your code by handling courses that do not exist for example

I would also highly recommend to teach yourself these thinking steps, to break the problem down into small steps, much better in the long run then relying on a walk-through video.

I knew…I just KNEW it would be right there in my face. I re did this thing time after time! Things like this tickle me in a way I can’t describe.

This is the only time i’ve used the video. I’m going to go over this lesson again but I wasn’t going to put this much time into it only to turn away. I had to see what I was doing wrong.

Thanks for your help, i’m going to keep that code snippet to help me with future errors!

What I hope the most, is that you teach yourself the steps that i just did. How did i discover the problem?

1 Like

I think that you solved it by trying to isolate what the problem was from within the function?

If you look at your error message it will say that something is undefined.
You would then look at where you meant to define it, or maybe you meant to reference something else.

You sit down.
There’s no chair. You crash.
Did you mean to sit down somewhere else, or did you mean to put a chair there? Because you did have a plan involving a chair when deciding to sit down, right?

This is not different from:

console.log(x) // x is undefined

…well a little different, because undefined is a value, and x is a variable that doesn’t refer to anything, not even undefined.
But it’s the same situation, if you try to use something you should probably be able to argue for why it would be there, and if it’s not then fix it.

So, no, you wouldn’t find it because “it’s in your face.”
You’d find it because you’d make sure it’s there, deliberately.

yes and up to this point reference errors haven’t been an issue but with the introduction to getters and setters the variables became a little confusing.

I wouldn’t say that I move forward with complete confidence on preventing reference errors BUT I can now approach these errors in a different way.

Thanks to this post I was able to go back and fix/ understand the specific issues. Then went on to make a little project of my own based on the skeleton of the meals project.
Encountered the same reference errors BUT I was able to fix it this time =]]