Missing initializer

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(appetizers)  {
    this._courses.appetizers=appertizers
  },set appetizers(mains)  {
    this._courses.mains=mains
  },set desserts(desserts)  {
    this._courses.desserts=desserts
  }, get courses(){
    appetizers: this.appetizers
      mains: this.mains
        desserts: this.desserts
  }, adddishcourse(coursesname,dishname,dishprice) {
    const dish={
      name:dishname,
      price:dishprice,
    }
return this._courses(coursesname).push(dish)
  },
   randomDishFromCourse(coursesname){
    const dishes =this.courses[coursesname]
const randomIndex = Math.floor(Math.random()*dishes.length)
    return dishes[randomIndex]},
   generaterandommeal(){
  const appetizers = this.randomDishFromCourse('appertizers')
const mains = this.randomDishFromCourse('mains')
const desserts = this.randomDishFromCourse('desserts')
const total price= appetizers.price + mains.price + desserts.price; <= here it is showing missing initializer
return `your meals are ${appetizers},${mains},${desserts}. costs you ${totalprice}.`;
}
};

menu.adddishcourse('appetizers','wings',100)
menu.adddishcourse('appetizers','frenchfries',200)
menu.adddishcourse('appetizers','soup',150)

menu.adddishcourse('mains','butter chicken',500)
menu.adddishcourse('mains','korma',700)
menu.adddishcourse('mains','biryani',900)

menu.adddishcourse('desserts','icecream', 90)
menu.adddishcourse('desserts','gulabjamun', 50)
menu.adddishcourse('desserts','halwa', 80)

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

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:39
const total price= appetizers.price + mains.price + desserts.price;
      ^^^^^
SyntaxError: Missing initializer in const declaration
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)
 

Can you elaborate more on what problem you’re facing?

A thread about code formatting →

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(appetizers)  {
    this._courses.appetizers=appertizers
  },set appetizers(mains)  {
    this._courses.mains=mains
  },set desserts(desserts)  {
    this._courses.desserts=desserts
  }, get courses(){
    appetizers: this.appetizers
      mains: this.mains
        desserts: this.desserts
  }, adddishcourse(coursesname,dishname,dishprice) {
    const dish={
      name:dishname,
      price:dishprice,
    }
return this._courses(coursesname).push(dish)
  },
   randomDishFromCourse(coursesname){
    const dishes =this.courses[coursesname]
const randomIndex = Math.floor(Math.random()*dishes.length)
    return dishes[randomIndex]},
   generaterandommeal(){
  const appetizers = this.randomDishFromCourse('appertizers')
const mains = this.randomDishFromCourse('mains')
const desserts = this.randomDishFromCourse('desserts')
const total price= appetizers.price + mains.price + desserts.price;
return `your meals are ${appetizers},${mains},${desserts}. costs you ${totalprice}.`;
}
};

menu.adddishcourse('appetizers','wings',100)
menu.adddishcourse('appetizers','frenchfries',200)
menu.adddishcourse('appetizers','soup',150)

menu.adddishcourse('mains','butter chicken',500)
menu.adddishcourse('mains','korma',700)
menu.adddishcourse('mains','biryani',900)

menu.adddishcourse('desserts','icecream', 90)
menu.adddishcourse('desserts','gulabjamun', 50)
menu.adddishcourse('desserts','halwa', 80)

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

HINT: You have naming errors, please check.

1 Like