Meal Maker project - object

Hello

My question is regarding the Meal Maker project: https://www.codecademy.com/courses/introduction-to-javascript/projects/meal-maker

I’ve watched the instruction video and there are a lot of setters and getters in this project. However, these are redundant because I afterwards I have commented them out and the code works fine. So my question is why are they included if they are not used or explained in the video?

An additional comment regarding this Meal Maker project. I find it very difficult to grasp how this code works. and produces the final result - randomly producing a meal. It seems like the exercises and learning before this project, about objects are insufficient for me to understand what is being done, but even more vital what is the purpose of each method applied and how they work togerther.

Uhm. Yeah. The getters and setters in that project do absolutely nothing, it is effectively dead code that should be pruned away. I guess the author wanted to show them but didn’t have a problem that they solve.

So the rest of the code would be much easier to get an understanding of after first cleaning out the getters and setters.

“object” is for the most part synonymous with “value”, for javascript it may sometimes specifically refer to this key-value mapping:

{ key1: value1, key2: value2 }

Presumably you’d get a random meal by picking a random course from each list of courses?

1 Like

Thank you for your reply.

I guess my need is a more in-depth understanding of the created object named meal. Thus, it’s property _courses and the methods used: addDishToCourse(courseName, dishName, dishPrice), getRandomDishFromCourse(courseName) and generateRandomMeal().

The way they are set up is not easy to understand.

Not sure there’s anything particularly in-depth to know. It’s lists (array) and key-value mappings (object), isn’t it. Maybe you need to isolate the thing you’re actually wondering about, refining the the question you’re asking and then you might be able to answer it yourself too once the question is a bit more concrete.

Perhaps you have something like this:

stuff = {
  courses: {
    desserts: [],
    main: [],
  }
}

which is nothing but objects and arrays, nothing new in that.

This is how the code looks like:

const menu = {

    _courses : {

      appetizers : [],

      mains : [],

      desserts : []

    },`Preformatted text`
//is adding dishes to empty arrays in_courses

    addDishToCourse(courseName, dishName, dishPrice){

      const dish = {

        name: dishName,

        price: dishPrice

      };

      return this._courses[courseName].push(dish);

    },

    //picks one of the three random dishes

    getRandomDishFromCourse(courseName) {

      const dishes = this._courses[courseName];      

      const randomIndex = Math.floor(Math.random()*dishes.length);

      return dishes[randomIndex];

      

    },

    // is the calling method of the program. Uses the getRandomDishFromCourse() to generate a random dish and price

    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}, and ${dessert.name} and the total price is ${totalPrice}`;

    }

  };

  

  menu.addDishToCourse('appetizers', 'salat', 4.00)

  menu.addDishToCourse('appetizers', 'wings', 5.00)

  menu.addDishToCourse('appetizers', 'fries', 3.00)

  

  menu.addDishToCourse('mains', 'salmon', 8.00)

  menu.addDishToCourse('mains', 'filet', 6.00)

  menu.addDishToCourse('mains', 'burger', 7.00)

  

  menu.addDishToCourse('desserts', 'coffee', 2.00)

  menu.addDishToCourse('desserts', 'pie', 4.50)

  menu.addDishToCourse('desserts', 'cake', 5.40)

  

  const meal = menu.generateRandomMeal();

  console.log(meal)

I don’t understand the addDishToCourse() method. Why make that object inside, the one named dish? Why not just return return this._courses[courseName].push(dishName, dishPrice);? (this doen’t work). Normaly you push values into an array, not objects

If you consider the purpose of addDishToCourse then it probably shouldn’t return anything at all, should it? And if you consider the purpose of it, again, that might say something about why an object should be created.

Well, I guess if an object is required to give addDishToCourse a purpose it serves it’s goal, but I cannot learn anything from that - maybe people with js experience can.

In order to add a meal, you’d need to represent the meal as a value that can be placed in the list