ChronicLogic
ChronicLogic

Reputation: 352

broken code at .push in JS.

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!

Upvotes: 0

Views: 58

Answers (1)

Andy
Andy

Reputation: 63524

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.

Upvotes: 1

Related Questions