Meal Maker Project help

Continuing the discussion from Meal Maker JS Project - help needed :slight_smile::

I had a similar problem but my code uses the assignment =, can someone tell me why my code always returns the string ‘Meal or price was not set correctly!’ ?

const menu = {
 _meal : '', 
 _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 Special is ${this._meal} for ${this._price}!`}
    else {
      return 'Meal or price was not set correctly!'
    }
 }
};

menu.meal = 'lasagna';
menu.price = 11;

console.log(menu.todaysSpecial);

In the snippet

// You wrote:
set price (priceToCheck) {
  if (typeof (priceTocheck) === 'number')

you wrote priceTocheck instead of priceToCheck. Consequently, the condition is false ("undefined" is not equal to "number") and this._price remains unchanged from its initial assigned value of 0.

Even though priceTocheck hasn’t been declared anywhere in your code, no error is being thrown because you are using it with the typeof operator. See:

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof#interaction_with_undeclared_and_uninitialized_variables

1 Like

Thank you this was driving me nuts!

1 Like