Hi everyone, could anyone please help me find out the issue
Code:`const menu = {
_menu: ‘’,
_price: 0,
set meal(mealToCheck) {
if (typeof mealToCheck === ‘string’) {
return this._meal = mealToCheck;
},
set price(priceToCheck) {
if (typeof priceToCheck === ‘number’) {
return this.price = priceToCheck;
}
},
get todaysSpecial() {
if (this._meal && this._price) {
return Today's Meal is ${this._meal} for $${this._price}!
} else {
return Meal or price not set correctly!
}
}
describe the issue you’re facing – what do you expect and what do you get instead
format your code
You will see an error in the console. Posting it here would help others and yourself tracing the error.
In your case the issue is referencing undefined variables with this keyword. Not everything you’re trying to return is defined as an object property.
SyntaxError: Unexpected token ‘,’
at wrapSafe (internal/modules/cjs/loader.js:1001:16)
at Module._compile (internal/modules/cjs/loader.js:1049:27)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
at internal/main/run_main_module.js:17:47
Code:
`const menu = {
_menu: ‘’,
_price: 0,
set meal(mealToCheck) {
if (typeof mealToCheck === ‘string’) {
return this._meal = mealToCheck;
},
set price(priceToCheck) {
if (typeof priceToCheck === ‘number’) {
return this.price = priceToCheck;
}
},
get todaysSpecial() {
if (this._meal && this._price) {
return Today's Meal is ${this._meal} for $${this._price}!
} else {
return Meal or price not set correctly!
}
}
I really recommend you to read the guide. The more effort you put in your post, the more likely it is that someone else puts some effort in reading it.
Pay some attention to these issues:
I did already point out a problem in your code:
Check which object properties you have defined and if they match with the properties you reference (they don’t).
You used another symbol than the quote. For example, your choice is - ‘’. Should use - ‘’. (looks almost similar) This is the button next to the colon, to the right of it.
In this snippet of code you omitted the backticks on each side of executed code.
*your variation.
get todaysSpecial() {
if (this._meal && this._price) {
return Today's Meal is ${this._meal} for $${this._price}!
} else {
return Meal or price not set correctly!
}
}
*should be
get todaysSpecial() {
if (this._meal && this._price) {
return `Today's Meal is ${this._meal} for $${this._price}!`
} else {
return `Meal or price not set correctly!`
}
}
It shows that you miss 1 closed curly brace after your first fragment of setter :
set meal(mealToCheck) {
if (typeof mealToCheck === 'string') {
return this._meal = mealToCheck;
}
},
And the last moment, inside of the second setter you need to write underscore (_) before the returned property.
Your variation is “return this.price = priceToCheck;”
Should be “return this._price = priceToCheck;”
Here’s the full correct code:
const menu = {
_menu: '',
_price: 0,
set meal(mealToCheck) {
if (typeof mealToCheck === 'string') {
return this._meal = mealToCheck;
}
},
set price(priceToCheck) {
if (typeof priceToCheck === 'number') {
return this._price = priceToCheck;
}
},
get todaysSpecial() {
if (this._meal && this._price) {
return `Today's Meal is ${this._meal} for $${this._price}!`
} else {
return `Meal or price not set correctly!`
}
}
};
menu.meal = 'Pizza';
menu.price = 8;
console.log(menu.todaysSpecial);