Meal Maker step 4


#1

All project ->

const menu = {
  _courses: {
		appetizers: [],
		mains: [],
    desserts: [],
    },
  get courses() {
		return {
    appetizers: 
   this._courses.appetizers,
    mains: 
   this._courses.mains,
    deserts: 
   this._courses.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;
  	}, 
	addDishToCourse(courseName,dishName,dishPrice){
  const dish = {
		name: dishName,
  	price: dishPrice
		};
    
		this._courses[courseName].push(dish);
    //this._courses[courseName] = dish;
    //console.log(dish.price);
    //console.log(this._courses[courseName])
	},
  getRandomDishFromCourse(courseName) {
    const dishes = this._courses[courseName];
    //console.log(dishes);
    const randomIndex = Math.floor(Math.random() * dishes.length);
    //console.log(dishes[randomIndex]);
    return dishes[2]; 
  },
  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}, ${dessert.name}. The price is $${totalPrice + 0.0}.`; 
	},
};
menu.addDishToCourse('mains', 'pizza', 5);
menu.addDishToCourse('mains', 'sandwich', 4.0);
menu.addDishToCourse('mains', 'nachos', 6);

menu.addDishToCourse('appetizers', 'chips', 2.00);
menu.addDishToCourse('appetizers', 'veggies', 3.0);
menu.addDishToCourse('appetizers', 'onion rings', 3.00);

menu.addDishToCourse('desserts', 'cake', 2.00);
menu.addDishToCourse('desserts', 'ice cream', 3.00);
menu.addDishToCourse('desserts', 'fruit', 4);
let meal = menu.generateRandomMeal();
console.log(meal);

Why do I need this code? Without it, the project works the same way.

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;
  	},

Help to understand please.


#2

Those are used instead of the ‘addDishToCourse’ method.

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: []
  },

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

  set appetizers (appetizer) {
    this._courses.appetizers.push(appetizer)
  },

  set mains (main) {
    this._courses.mains.push(main)
  },

  set desserts (dessert) {
    this._courses.desserts.push(dessert)
  },

  get appetizers () {
    return this._courses.appetizers
  },

  get mains () {
    return this._courses.mains
  },

  get desserts () {
    return this._courses.desserts
  },

  getRandomDishFromCourse (type) {
    const dishes = this._courses[type]
    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}, ${dessert.name}. The price is $${totalPrice + 0.0}.`
  }
}

And can be used like this:

menu.mains = { name: 'pizza', price: 5.00 }
menu.mains = { name: 'nachos', price: 6.00 }
menu.mains = { name: 'sandwich', price: 4.00 }

menu.appetizers = { name: 'chips', price: 2.00 }
menu.appetizers = { name: 'veggies', price: 3.00 }
menu.appetizers = { name: 'onion rings', price: 3.00 }

menu.desserts = { name: 'cake', price: 2.00 }
menu.desserts = { name: 'ice cream', price: 3.00 }
menu.desserts = { name: 'fruit', price: 4.00 }

console.log(menu.courses)
console.log(menu.mains)
console.log(menu.appetizers)
console.log(menu.desserts)

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

More information about setter and getter can be found on the Mozilla Developer site;
setter - getter


#3

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