Home > database >  Went over the whole thing several times but still can't find the mistake
Went over the whole thing several times but still can't find the mistake

Time:02-12

This code is from a tutorial. It kept giving weird errors and now it's stuck at

SyntaxError: Unexpected token }

I followed the tutorial closely but I still can't locate the error. P.S Apparently I need to add a certain number of words to the description to be able to submit the question so this is just me doing that, I really have nothing else to say. Thank you.

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(appt) {
    return this._courses.appetizers = appetizers;
  },
  set mains(mains) {
    return this._courses.mains = mains;
  },
  set desserts(dess) {
    return this._courses.desserts = desserts,
  },
  get courses() {
    return {
      appetizers: this._courses.appetizers;
      mains: this._courses.mains;
      desserts: this._courses.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 randomI = Math.floor(Math.random() * dishes.length);
    return dishes[randomI];
  },
  generateRandomMeal() {
    const appetizer = this.getRandomDishFromCourse(appetizers);
    const main = this.getRandomDishFromCourse(main);
    const dessert = this.getRandomDishFromCourse(desserts);
    const totalPrice = appetizer.price   main.price   dessert.price;
    return `Your mean is ${appetizer.name}, ${main.name} and ${dessert.name}, and the total price is ${totalPrice}`
  },
};



menu.addDishToCourse('appetizers', 'salad', 4)
menu.addDishToCourse('appetizers', 'wings', 3)
menu.addDishToCourse('appetizers', 'fries', 5)

menu.addDishToCourse('mains', 'steak', 10)
menu.addDishToCourse('mains', 'kebab', 11)
menu.addDishToCourse('mains', 'cizbiz', 9)

menu.addDishToCourse('desserts', 'ice cream', 4)
menu.addDishToCourse('desserts', 'cake', 5)
menu.addDishToCourse('desserts', 'coffee', 4)


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

CodePudding user response:

Ok, so I have found many errors here

line 17 should be: return this._courses.appetizers = appt;

appt is the argument passed in to the function

line 23 should be: return this._courses.desserts = dess;

Again, dess is the argument passed in to the function. Also, the statement needs to end witha semicolon, not a comma.

lines 27 - 29: The lines need to end with commas and not semicolons. It is returning an object, so the properties of the object need to be seperated by commas, not semicolons.

lines 34 - 35: Same issue, returning an object. Need to seperate properties with commas instead of semicolons.

lines 46 - 48: The variables passed into the functions do not exist. The arguments for that function should be strings. So change those variables to strings instead. Also, make sure to change main to "mains" on line 48.

You should learn some debugging. Either run the code with Node.js or put it in a script tag in HTML and run it in the browser. Then you can see the errors produced which usually show you exactly where the problems are.

  • Related