Project Meal Maker - HELP PLEASE


#1

Can someone help me please?

What i’m doing wrong in the addDishToCourse function? Line 37.

https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/learn-javascript-objects/projects/meal-maker

const menu = {
  _courses: {
    appetizers: [],
 		mains: [],
    desserts: []
  },
  
  get appetizersM () {
    return this._courses.appetizers;
  },
  set appetizersM (appetizersIn) {
    this._courses.appetizers.push(appetizersIn);
  },
  
  get mainsM () {
    return this._courses.mains;
  },
  set mainsM (mainsIn) {
    this._courses.mains.push(mainsIn);
  },
  
  get dessertsM () {
    return this._courses.desserts;
  },
  set dessertsM (dessertsIn) {
 	 this._courses.desserts.push(dessertsIn);
  },
  
  get courses () {
    return {
      appetizers: this._courses.appetizersM,
      mains: this._courses.mainsM,
      desserts: this._courses.dessertsM,
    }
  },
  
  addDishToCourse: function (courseName, dishName, dishPrice) {
    const dish = {
      name: dishName, 
      price: dishPrice
    };
    //My Solution is WRONG Why?? 
    // I want to use my setter method!!
  if (courseName === 'appetizers') {
     this._courses.appetizersM = dish;
    } else if (courseName === 'mains') {
      this._courses.mainsM = dish;
    } else if (courseName === 'desserts') {
     this._courses.dessertsM = dish;
    }
// Correct Solution 
//this._courses[courseName].push(dish);
    },
  
  getRandomDishFromCourse: function (courseName) {
    const dishes = this._courses[courseName];
    
	const computerChoice = Math.floor(Math.random() * dishes.length);
  
  return this._courses[courseName][computerChoice];
  },
  
  generateRandomMeal(){
    const appetizerOp = this.getRandomDishFromCourse('appetizers');
    const mainOp = this.getRandomDishFromCourse('mains');
    const dessertOp = this.getRandomDishFromCourse('desserts');
     
    const totalPrice = appetizerOp.price + mainOp.price + dessertOp.price;
  
return `Your meal is ${appetizerOp.name}, ${mainOp.name} and ${dessertOp.name}.The price is ${totalPrice}.`;
  }
};


//Let's test

menu.addDishToCourse('mains', 'Feijoada', 18);
menu.addDishToCourse('mains', 'Bacalhau', 20);
menu.addDishToCourse('mains', 'Dourada Grelhada', 22);
menu.addDishToCourse('desserts', 'Mousse', 3);
menu.addDishToCourse('desserts', 'Salada Fruta', 2);
menu.addDishToCourse('desserts', 'Gelado', 3.5);
menu.addDishToCourse('appetizers', 'Azeitonas e Pao Caseiro', 2);
menu.addDishToCourse('appetizers', 'Pao Alho', 4);
menu.addDishToCourse('appetizers', 'Tabua de Queijos e Pao', 5);

menu.dessertsM = {
      name: 'Baba Camelo', 
      price: 3
    };

console.log(menu.mainsM);
console.log(menu);

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

#2

Hi,
You did everything correctly, but:

if (courseName === 'appetizers') {
     this.appetizersM = dish;
    } else if (courseName === 'mains') {
      this.mainsM = dish;
    } else if (courseName === 'desserts') {
     this.dessertsM = dish;
    }

Use this syntax. You dont need ._courses, because you are reaching them directly in the menu object.

I guess you have been staring at it too much :slightly_smiling_face:


#3

Thanks you very much. Now i understand :sweat_smile: