Js, meal maker


#1
let menu = {
  _courses: {
    _appetizers: [],
    _mains: [],
    _desserts: [],
    
    set appetizers(appetizer) {
        this._appetizers.push(appetizer);
    },
    
    set mains(main) {
        this._mains.push(main);
    },
    
    set desserts(dessert) {
        this._desserts.push(dessert);
    },
    
    get appetizers() {
      return this._appetizers;
    },
    
    get mains() {
			return this._mains;
    },
    
    get desserts() {
    	return this._desserts;
  	}
  },
  
  get courses() {
    return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts
    }
  },
  
  addDishCourses(courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice
    };
    
    this._courses.courseName = 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 = appetizers.price + mains.price + desserts.price;
    
    return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name}.The price is $${totalPrice}.`;
  },
  
};


menu.addDishCourses('appetizers', 'soup', '$5');

console.log(menu.courses);


setter is not working and i can’t figure out why!
Pelase help me!


#2

here:

this._courses.courseName = dish;

courseName won’t work, its of type string, so you need to use square brackets:

[courseName]

also, this._courses.courseName = dish; will call the setter method (given we set a value), not the getter method.


#3

thank you, thought about that and didn’t try it. Now it is working!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.