Absolutely Lost On Objects

Hi, I’m really struggling to understand how objects work as well as finishing the Meal Maker project.

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  get appetizers() {
    return this._courses.appetizers;
  },
  set appetizers(appetizerIn) {
    this._courses.appetizers = appetizerIn;
  },
  get mains() {
    return this._courses.mains;
  },
  set mains(mainIn) {
    this._courses.mains = mainIn;
  },
  get desserts() {
    return this._courses.desserts;
  },
  set desserts(dessertIn) {
    return this._courses.desserts = dessertIn;
  },
  get courses() {
    return _courses;
  },
  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: courseName,
      price: dishPrice,
    };
    
    return this._courses[courseName].push(dish);
  },
  getRandomDishFromCourse(courseName) {
    const dishes = this._courses[courseName];
    const index = Math.floor(Math.random() * dishes.length);
    return dishes[index];
  },
  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 three-course meal is ${appetizers.name}, ${mains.name}, and ${desserts.name}. Your total price is ${totalPrice} dollars.`
  }
};

menu.addDishToCourse('appetizers', 'Chicken', 10);
menu.addDishToCourse('appetizers', 'Turkey', 12.50);
menu.addDishToCourse('appetizers', 'Salad', 8.50);
menu.addDishToCourse('mains', 'Burger', 15.99);
menu.addDishToCourse('mains', 'Salmon', 20.50);
menu.addDishToCourse('mains', 'Tacos', 11.99);
menu.addDishToCourse('desserts', 'Chocolate', 10);
menu.addDishToCourse('desserts', 'Ice Cream', 10);
menu.addDishToCourse('desserts', 'Hot Fudge', 10);

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

When I log meal to the console, it gives the right price but always prints appetizers, mains, and desserts instead of the course names. I’m also really struggling to understand how each function other than generateRandomMeal inside the menu object works, even after taking notes from the previous lesson. What have i done wrong, and how is menu supposed to work?

inserting console.log statement to see where you go wrong can help, so i started here:

generateRandomMeal() {
    const appetizers = this.getRandomDishFromCourse('appetizers');
    const mains = this.getRandomDishFromCourse('mains');
    const desserts = this.getRandomDishFromCourse('desserts');
    console.log(appetizers);
    const totalPrice = appetizers.price + mains.price + desserts.price;
    return `Your three-course meal is ${appetizers.name}, ${mains.name}, and ${desserts.name}. Your total price is ${totalPrice} dollars.`
  }

which showed me the the object, and it shows that the name is incorrect.

so then the next logic step would be to take a closer look at addDishToCourse method:

addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: courseName,
      price: dishPrice,
    };

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

oh oh, i think we found the problem :slight_smile:

normally i would use a debugger, but logging is easier to display on the forum

1 Like

Thank you so much for this. Another question I have pertains to the setter methods. What exactly was their purpose in the context of menu? Because They I don’t think they’re used in any of the other methods.

1 Like

you could use the setters to change the default behavior, that within the setter method you use .push()

or could use the setter for validation, but in the exercise they do not at much

You are correct, they are not used since there is the addDishToCourse method :wink: . Well spotted.