Use of getter and setter method in the project Meal Maker

Hello everyone, I hope everyone is doing well.

I have a quick question about the project Meal Maker from the JavaScript objects lesson: Meal Maker

First of all, here is my final working code:

const menu = {
  _courses: {
    appetizer: [],
    main: [],
    //sidedish: [],
    dessert: [],
 }, 
   //Getter method
  get appetizerGet () {
    return this._courses.appetizer
  },  
  get mainGet () {
    return this._courses.main
  },
  get dessertGet () {
    return this._courses.dessert
  }, 
  //Set Method
  set appetizer (appetizer) {
    this._courses.appetizer = appetizer;
  },
  set main (main) {
    this._courses.main = main;
  },
  set dessert (dessert) {
    this._courses.dessert = dessert;
  },
  //Assign Meals
  addMeals (course, name, price) {
    const dishAttributes = { name,  price, }                  
    if(course.toLowerCase() === 'appetizer') {
      this._courses.appetizer.push(dishAttributes)
    } else if (course.toLowerCase() === 'main') {
      this._courses.main.push(dishAttributes)
    } else if (course.toLowerCase() === 'dessert') {
      this._courses.dessert.push(dishAttributes)
    };
  },
  //Random Dish
  getRandomDish (course) {
    const appetizers = this.appetizerGet;
    const mains = this.mainGet;
    const desserts = this.dessertGet;
    //correct use of Getter method?
    if(course === 'appetizer') {
      return appetizers[Math.floor(Math.random() * appetizers.length)]
    } else if (course === 'main') {
      return mains[Math.floor(Math.random() * mains.length)]
    } else if (course === 'dessert')
      return desserts[Math.floor(Math.random() * desserts.length)]
  },
  //Random Meals
  generateRandomMeal () {
    const appetizer = this.getRandomDish('appetizer');
    const main = this.getRandomDish('main');
    const dessert = this.getRandomDish('dessert');
    const totalPrice = appetizer.price + main.price + dessert.price;
    return `Welcome!! Today, we have ${appetizer.name} for your Appetizer, ${main.name} for your main dish and finally  ${dessert.name} for your dessert. Thank you for coming, your total is ${Math.floor(totalPrice) }`;
  },
}
//ASSIGNING MEALS
menu.addMeals('main', 'something', 20.00)
menu.addMeals('main', '1', 15.00)
menu.addMeals('main', '2', 9.99)
menu.addMeals('appetizer', '3', 8.99)
menu.addMeals('appetizer', '4', 5.00)
menu.addMeals('appetizer', '5', 2.99)
menu.addMeals('dessert', '6', 2.99)
menu.addMeals('dessert', '7', 3.99)
menu.addMeals('dessert', '8', 2.99)

const finalGeneratedDish = menu.generateRandomMeal()

console.log(finalGeneratedDish)


Second, the main focus of the question:

//Random Dish
  getRandomDish (course) {
    const appetizers = this.appetizerGet;
    const mains = this.mainGet;
    const desserts = this.dessertGet;
    //correct use of Getter method?
    if(course === 'appetizer') {
      return appetizers[Math.floor(Math.random() * appetizers.length)]
    } else if (course === 'main') {
      return mains[Math.floor(Math.random() * mains.length)]
    } else if (course === 'dessert')
      return desserts[Math.floor(Math.random() * desserts.length)]
  },

Does the code above use the getter Method as it was intended to do so or is there any other way to write this code as if in a shorter form? One more thing, I don’t see the step by step instructions telling us when to use the remaining 3 setter methods or did I miss something? I will really appreciate all the help!

Hi @harsh.chane,

Really well done on completing the project!

To address your first question, the method that you have written will serve the purpose you intend and is therefore just fine for the job. If, however, you were looking for a more concise way to find the same output I would suggest the following:

rename your getter methods at the top to remove the word ‘Get’ from the end - like so:

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

You would then be able to write the getRandomDish method in this way:


  getRandomDish(course) {
    let dishes = this[course];
    let randomIndex = Math.floor(Math.random() * dishes.length);
    return dishes[randomIndex];
  },

On your second question, you should be able to use your setter methods in your addMeals method instead of writing out the full expressions:

this._courses.appetizer.push(dishAttributes)

could be:

this[main] = dishAttributes;

Once again, good job!