Meal Maker Problems


#1

Cannot read dishPrice? Have no idea what could be the issue. Any help appreciated, code below.

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  //Get
  get appetizers(){
    
  },
  get mains() {
    
  },
  get desserts() {
    
  },
  get courses() {
    return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts,
    }
  },
  //Set
  set appetizers(appetizerIn) {
    
  },
  set mains(mainIn) {
    
  },
  set desserts(dessertsIn) {
    
  },
  addDishToCourse (courseName, dishName, dishPrice) {
    let dish = {
      courseName,
      dishName,
      dishPrice,
    };
    this._courses[courseName].push(dish);
  },
  getRandomDishFromCourse(courseName) {
    const dishes = this._courses[courseName];
    const randomIndex = Math.floor(Math.random() * dishes.length);
  },
  generateRandomMeal() {
    const appetizer = this.getRandomDishFromCourse('appetizers');
    const mains = this.getRandomDishFromCourse('mains');
    const desserts = this.getRandomDishFromCourse('desserts');
    const totalPrice = appetizer.dishPrice + mains.dishPrice + desserts.dishPrice;
    return `Your meal is ${appetizer.name}, ${mains.name}, ${desserts.name}. The price is ${totalPrice}.`;
  }
};
menu.addDishToCourse('appetizers', 'salad', 8);
menu.addDishToCourse('appetizers', 'onion rings', 11);
menu.addDishToCourse('appetizers', 'shrimp', 13);
menu.addDishToCourse('mains', 'chicken', 18);
menu.addDishToCourse('mains', 'beef', 16);
menu.addDishToCourse('mains', 'chili', 14);
menu.addDishToCourse('desserts', 'fudge brownie', 7);
menu.addDishToCourse('desserts', 'dolce de leche', 5);
menu.addDishToCourse('desserts', 'cake shake', 4);

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

#2
    let dish = {
      courseName,
      dishName,
      dishPrice,
    };

The above is not an object.


#3

Now getting undefined for the meals.

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  //Get
  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,
    }
  },
  //Set
  set appetizers(appetizerIn) {
    this._appetizers.push(appetizer);
  },
  set mains(mainIn) {
    this._mains.push(main);
  },
  set desserts(dessertsIn) {
    this._desserts.push(dessert);
  },
  addDishToCourse (courseName, dishName, dishPrice) {
    let dish = {
      courseName: courseName,
      dishName: dishName,
      dishPrice: dishPrice,
    }
    this._courses[courseName].push(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 mains = this.getRandomDishFromCourse('mains');
    const desserts = this.getRandomDishFromCourse('desserts');
    const totalPrice = appetizer.dishPrice + mains.dishPrice + desserts.dishPrice;
    return `Your meal is ${appetizer.name}, ${mains.name}, ${desserts.name}. The price is ${totalPrice}.`;
  }
};
menu.addDishToCourse('appetizers', 'salad', 8);
menu.addDishToCourse('appetizers', 'onion rings', 11);
menu.addDishToCourse('appetizers', 'shrimp', 13);
menu.addDishToCourse('mains', 'chicken', 18);
menu.addDishToCourse('mains', 'beef', 16);
menu.addDishToCourse('mains', 'chili', 14);
menu.addDishToCourse('desserts', 'fudge brownie', 7);
menu.addDishToCourse('desserts', 'dolce de leche', 5);
menu.addDishToCourse('desserts', 'cake shake', 4);

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

#4
 set appetizers(appetizerIn) {
    this._appetizers.push(appetizer);
  },
  set mains(mainIn) {
    this._mains.push(main);
  },
  set desserts(dessertsIn) {
    this._desserts.push(dessert);

Drop the In from the parameter names so the variables agree.


#5

Finally got the code working 100%. Completed version below.

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  //Get
  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,
    }
  },
  //Set
  set appetizers(appetizer) {
    this._appetizers.push(appetizer);
  },
  set mains(main) {
    this._mains.push(main);
  },
  set desserts(dessert) {
    this._desserts.push(dessert);
  },
  addDishToCourse(courseName, dishName, dishPrice) {
    let dish =  {
      courseName: courseName,
      dishName: dishName,
      dishPrice: dishPrice,
    };
    this._courses[courseName].push(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 = appetizer.dishPrice + main.dishPrice + dessert.dishPrice;
    return `Your meal is ${appetizer.dishName}, ${main.dishName}, ${dessert.dishName}. The price is ${totalPrice}.`;
  }
};
menu.addDishToCourse('appetizers', 'salad', 8);
menu.addDishToCourse('appetizers', 'onion rings', 11);
menu.addDishToCourse('appetizers', 'shrimp', 13);
menu.addDishToCourse('mains', 'chicken', 18);
menu.addDishToCourse('mains', 'beef', 16);
menu.addDishToCourse('mains', 'chili', 14);
menu.addDishToCourse('desserts', 'fudge brownie', 7);
menu.addDishToCourse('desserts', 'dolce de leche', 5);
menu.addDishToCourse('desserts', 'cake shake', 4);

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

#6

Since the courseName referenced property has a setter, try,

this._courses[courseName] = dish;

What’s more,

    let dish =  {
      courseName: courseName,
      dishName: dishName,
      dishPrice: dishPrice,
    };

only needs two properties.

  addDishToCourse (courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice
    };
    this._courses[courseName] = dish;
  },  

In my code I opted for simpler key names in this object. That’s your call.


#7

Good to know. I like to save the finished product and go back to rewrite until it becomes like second nature. I will take a look into the code adjustments you recommended and apply to see how it can make it easier in the future. Trying to get through JavaScript and continue on with Web Development. Thank You for your time.


#8

You’re welcome. Here’s the project link for when you return to review…

Meal Maker