Meal Maker (but also about Objects in general?)

Hi everyone, I was struggling with the Meal Maker project until I realized my problem was that I had inconsistently written things as ‘appetizers’ and ‘appetizer’ throughout the code. My function then worked, but it left me wondering:

Why does our key need to be a string in order to access it? Is this an intrinsic way that objects work and I have totally forgotten?

  generateRandomMeal() {
    const appetizer = this.getRandomDishFromCourse('appetizers');
    const main = this.getRandomDishFromCourse('mains');
    const dessert =  this.getRandomDishFromCourse('desserts');
    const totalPrice = appetizer.price + main.price + dessert.price;

    console.log(`Your meal is ${appetizer.name}, ${main.name}, and ${dessert.name}. That will be $${totalPrice}.`);
  }
};

VS

 generateRandomMeal() {
    const appetizer = this.getRandomDishFromCourse(appetizers);
    const main = this.getRandomDishFromCourse(mains);
    const dessert =  this.getRandomDishFromCourse(desserts);
    const totalPrice = appetizer.price + main.price + dessert.price;

    console.log(`Your meal is ${appetizer.name}, ${main.name}, and ${dessert.name}. That will be $${totalPrice}.`);
  }
};

(The difference is I took the ’ ’ off of the words appetizers, mains, desserts, etc.)

Similarly when accessing it from getRandomDishFromCourse(), why do we declare

 getRandomDishFromCourse(courseName) {
    const dishes = this._courses[courseName];
    const randomIndex = Math.floor(Math.random() * dishes.length);
    return dishes[randomIndex];
  },

instead of
const dishes = this._courses.courseName;
?

Any help would be appreciated. :slight_smile:
Full code in details if it helps.

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

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

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

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

  set appetizers(appetizerIn) {
    this._courses.appetizers = appetizerIn;
  },

  set mains(mainsIn) {
    this._courses.mains = mainsIn;
  },

  set desserts(dessertsIn) {
    this._courses.desserts = dessertsIn;
  },

  get courses() {
    return _courses;
  },

  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = { 
      name: dishName,
      price: dishPrice, 
    };
    return 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.price + main.price + dessert.price;

    console.log(`Your meal is ${appetizer.name}, ${main.name}, and ${dessert.name}. That will be $${totalPrice}.`);
  }
};

menu.addDishToCourse('appetizers', 'jalapeno poppers', 5);
menu.addDishToCourse('appetizers', 'onion rings', 4);
menu.addDishToCourse('appetizers', 'freedom fries', 3);

menu.addDishToCourse('mains', 'steak', 16);
menu.addDishToCourse('mains', 'borger', 13);
menu.addDishToCourse('mains', 'salads', 10);

menu.addDishToCourse('desserts', 'shake', 5);
menu.addDishToCourse('desserts', 'cake', 6);
menu.addDishToCourse('desserts', 'ice cream', 4);

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

Is there a global with that name? No. Since we are addressing an object key, which is a string, it follows we would pass a string to that parameter.

1 Like

The following uses the variable from the function provided(getRandomDishFromCourse) to look for the property provided in the _course object.


getRandomDishFromCourse(courseName) {
  const dishes = this._courses[courseName];

If you did this

getRandomDishFromCourse(courseName) {
  const dishes = this._courses.courseName;

It’s like saying hey lookup the property courseName instead of something like appetizers or main in the example provided in the summary. _courses.courseName doesn’t exist. So that’s one answer.

  1. The key needs to be a string because you are accessing the hash table of the object. objects are basically json notation but we don’t have to quote the key of a property when writing it as a property.
1 Like

Thank you! This helps a lot.