0

I cant seem to find why I cannot push my dish to courses[courseName] in the addDishToCourse method.. courseName is an array, so there shouldn't be any issues (although, factually, that't not true ><). Please, help?

const menu = {
    _courses : {
      _appetizers : [],
      _mains : [],
      _desserts : []
    },

>>    set appetizers(appetizerIn) {

    },
    get appetizers() {

    },
    set mains(mainIn) {

    },
    get mains() {

    },
    set desserts(dessertIn) {

    },
    get desserts() {

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

    },
        //Below is where my code breaks with the .push 

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

    },
    getRandomDishFromCourse(courseName) {
      const dishes = this._courses[courseName];
      const randomIndex = Math.floor(Math.random() * this.dishes.length);
      return dishes[randomIndex];
    },

    generateRandomMeal() {
      const appetizer = this.getRandomDishFromCourse('appetizers');
      const main = this.getRandomDishFromCourse('mains');
      const dessert = this.getRandomDishFromCourse('desserts');
      //const totalPrice = appetizers.price + mains.price + desserts.price;

      return `Your appetizer is ${appetizers.name} followed by the main meal, which will be ${mains.name}, and finally you will have ${desserts.name} for dessert.`;// Your bill will be of ${totalPrice}.`;
    }

  };

  menu.addDishToCourse('appetizers', 'Caesar Salad', 3.75);
  menu.addDishToCourse('appetizers', 'Srimp Cocktail', 6.50);
  menu.addDishToCourse('appetizers', 'Escargots Gratines', 4.50);
  menu.addDishToCourse('mains', '16oz Ribeye', 38);
  menu.addDishToCourse('mains', 'Smoked Salmon', 18);
  menu.addDishToCourse('mains', 'Grilled Chicken Breast', 19);
  menu.addDishToCourse('desserts', 'Chocolate Lava Cake', 3.50);
  menu.addDishToCourse('desserts', 'Tiramisu', 4);
  menu.addDishToCourse('desserts', 'Kiev Cake', 6.50);

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

Now I tried using an if statement to get around the .push, but that just broke the code elsewhere. Otherwise, I know that I potentially use a setter method, but I'm not too sure on how to deal with that either. Would I set the addDishToCourse?

I am a novice (as seen by the simplistic code^_^) so any help would be greatly appreciated!

3
  • 1
    "_something" !== "something" Commented Dec 14, 2017 at 10:13
  • Not related to this issue, but IMHO your getter functions should return copies of the arrays, and your setters should likewise copy the passed array into the local array. Otherwise any client of this class that obtains a reference to your internal data can modify it in place and break your object's encapsulation. Commented Dec 14, 2017 at 10:18
  • thanks @Alnitak ! I'll definitely be modifying my setters/getters once I'm more comfortable in them. I never thought of that, and it will be useful for the future ^_^ Commented Dec 14, 2017 at 10:29

1 Answer 1

1

When you create your new instance your courseName needs to match the name of the key in your _courses object. In your code all of these names are preceded by an underscore so the code breaks.

For example:

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

You're passing in "appetizers" as the course name...

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

...and you're trying to add "appetizers" to _courses, but _courses only has the _appetizers key name.

I would change the name of your object keys to appetizer, mains, and dessert respectively.

Sign up to request clarification or add additional context in comments.

2 Comments

Can I somehow not alter the _courses keys and point them to setters/getters with their respective names without the _ and then call on them? Also, tried taking away the _ in the key values, and now I have a break in my .legth method heh. so I changed it for dishes.length but then my code broke in ${appetizers.name} so I changed it to ${menu._courses.appetizers.name} and now I get the phrase "Your appetizer is ..." but all the values are undefined. I'm really sorry if I'm being too needy :( I'm just trying to learn off the books
Isn't the underscore on the _courses object enough? Why do you need them on the key names too?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.