JS newbie – Setters

Hello! Long time ... 🫠 So why is this get function, when called at the end through console not printing Burger and 40? The setters are called with these values before. Part of codecademy js intro course
menu = {
_meal: '',
_price: 0,
set meal(mealToCheck) {
if (typeof mealToCheck === '') {
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 Special is ${this._meal} for ${this._price}$."
} else {
return 'Meal or price was not set correctly!'
}
}
}

menu._meal = 'Burger';
menu._price = 40;

console.log(menu);
console.log(menu.todaysSpecial);
menu = {
_meal: '',
_price: 0,
set meal(mealToCheck) {
if (typeof mealToCheck === '') {
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 Special is ${this._meal} for ${this._price}$."
} else {
return 'Meal or price was not set correctly!'
}
}
}

menu._meal = 'Burger';
menu._price = 40;

console.log(menu);
console.log(menu.todaysSpecial);
4 Replies
Å Marlon G
Å Marlon GOP2y ago
Now it returns Today's Special is ${this._meal} for ${this._price}$.
Jochem
Jochem2y ago
there's two things. You're setting the properties directly, you should be using menu.meal = 40; without the _, and you're trying to use a template string without using backticks.
menu = {
_meal: '',
_price: 0,
set meal(mealToCheck) {
if (typeof mealToCheck === '') {
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 Special is ${this._meal} for ${this._price}$.`
} else {
return 'Meal or price was not set correctly!'
}
}
}

menu.meal = 'Burger';
menu.price = 40;

console.log(menu);
console.log(menu.todaysSpecial);
menu = {
_meal: '',
_price: 0,
set meal(mealToCheck) {
if (typeof mealToCheck === '') {
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 Special is ${this._meal} for ${this._price}$.`
} else {
return 'Meal or price was not set correctly!'
}
}
}

menu.meal = 'Burger';
menu.price = 40;

console.log(menu);
console.log(menu.todaysSpecial);
see how the syntax highlighting picks up the variables in this version?
Å Marlon G
Å Marlon GOP2y ago
... god damn! Changing the ticks and everything works. I did string ticks first, then got an error message since Today's has a special sign, and then changed it to quotation marks, because that's worked before. Easy one, this time. 😉 Thank you! 🙌
Jochem
Jochem2y ago
no problem, glad it was an easy fix 😄
Want results from more Discord servers?
Add your server