Meal Maker (JS)

Hello,

I’m struggling a bit with the mentioned project. The code is below, I also have a few questions.

First of all: is it ok to create a new topic for these projects? I was considering posting in others’ threads, however, it could make conversation there more convoluted. What’s the most common and desirable practice?

Moving to the code itself; I’ve written it with the help of an explainer video, however, it might not be 100% comparable, as I’ve tried to have some degree of understanding what I do.

The error I get is below, what baffles me is the fact that the error refers to the second line of the code, which is similar to the one above.

My last question is the use of the gets and sets, why exactly are they necessary?

const main = this.getRandomDishFromCourse('mains'),
    ^^^^^
SyntaxError: Unexpected token const
``

``
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 mains(mains) {

    this._courses.mains = mains;

  },

  set appetizers(appetizers) {

    this._courses.appetizers = appetizers;

  },

  set desserts(desserts) {

    this._courses.desserts = desserts;

  },

  get courses() {

    return {

      appetizers: this.appetizers,

      mains: this.mains,

      desserts: this.desserts,

    };

  },

 

  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;

    return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name} and the price is ${totalPrice}.`;

  } 

};

menu.addDishToCourse('appetizers', 'Bruschetta', 5);

menu.addDishToCourse('appetizers', 'Tatar', 15)

menu.addDishToCourse('appetizers', 'Onion Rings', 7.65)

menu.addDishToCourse('mains', 'Steak', 25);

menu.addDishToCourse('mains', 'Chicken Breast', 19);

menu.addDishToCourse('mains', 'Pizza', 12);

menu.addDishToCourse('desserts', 'Apple Pie', 9.90);

menu.addDishToCourse('desserts', 'Brownie', 11.90);

menu.addDishToCourse('desserts', 'Ice Cream', 7.90);

const meal = menu.generateRandomMeal();

console.log(meal);

You are referring to these two lines, I assume:

When the interpreter gets to the comma at the end of the first line, it is not expecting the next thing to be the keyword const. We don’t use commas to separate lines of code. We use them to separate items in an array or key: value pairs in an object, etc.

Getters and setters are special methods that belong to objects. They aren’t particularly necessary, but they are used by convention when we simply want to get or set a value for a property belonging to an object. We can use regular methods instead, but the keywords get and set make the methods that perform those functions easily identifiable to you and other programmers who may need to work with your code. Consider the following simplified example:

const someObject = {
  _someProperty: 'some value',
  _anotherProperty: 'another value',
    
  //getter and setter for _someProperty
  get someProperty() {
    return this._someProperty;
  },
  
  set someProperty(value) {
    this._someProperty = value;
  },
  
  //versus traditional methods for accessing _anotherProperty
  accessAnotherProperty() {
    return this._anotherProperty;
  },
  
  assignAnotherProperty(value) {
    this._anotherProperty = value;
  }
}
//So far there's little difference other than the use of the 
//keywords get and set. The main difference is in calling the methods:

//using the getter and setter to print, reassign and print again:
console.log(someObject.someProperty); //no () required to invoke the getter
someObject.someProperty = 'changed value'; //notice the syntax here
console.log(someObject.someProperty); //again, no () required to invoke the getter

//using traditional methods to perform the same operations as above:
console.log(someObject.accessAnotherProperty()); // have to use () to invoke the method
someObject.assignAnotherProperty('another changed vlaue'); //use () to invoke the setter, and pass the argument
console.log(someObject.accessAnotherProperty()); //again, must use () to invoke the method
1 Like