Getting error in Meal-Maker project in Javascript

Hi there, I am getting an error with this code(provided below). I’ve tried every possible thing I was able to think. I even asked chatGPT for the error, but it says this:

The code you provided is correct and should not result in the error you’re seeing. The error message “if (typeof mealToCheck === ‘string’)” suggests that there might be some inconsistency in the code execution environment or the way the code is being run.

Please make sure that you’re running the code in a proper JavaScript environment, and there are no modifications or typos in the code. If you’re still facing issues, try copying the code directly from the example I provided and running it again. If the issue persists, it might be related to the specific environment you’re using to run the code.

If you’re using a web-based coding platform or an online editor, there might be certain limitations or configurations that could affect the code execution. If possible, you could try running the code in a different JavaScript environment to see if the issue persists.

My Code:

const menu = {
  _meal: "",
  _price: 0,

  set _meal(mealToCheck) {
    if (typeof mealToCheck === "string") {
      this._meal = mealToCheck;
    }
  },
  set _price(priceToCheck) {
    if (typeof priceToCheck === "number") {
      this._price = priceToCheck;
    }
  },
  get todaysSpecial() {
    if (this._meal && this._price) {
      return `Today's Special is ${this._meal} for ${this._price}`;
    }
  },
};

menu._meal = "Sushi";
menu._price = 16;
console.log(menu.todaysSpecial);

Can anyone help? Would be of great help!

EDIT:

I then used this code, still getting the “/home/ccuser/workspace/learn-javascript-objects-meal-maker-update-2022/app.js:6
if (typeof mealToCheck === ‘string’) {
^”

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 `Todays Special is ${this._meal} for ${this._price}`
    } else {
      return 'Meal or price was not set correctly!'
    }
  }
}

menu._meal = 'Pizza'
menu._price = 8

console.log(menu.todaysSpecial)

The properties in the menu object are supposed to be _meal and _price. The underscore signifies that outside of the menu object, these properties are not meant to be accessed and altered directly i.e.

// Object
menu = { ... }

// From hereon we are outside the object

// We don't want the properties to be accessed and set directly.
// We don't want this. 
menu._meal = ...
menu._price = ...

// We want to use getters and setters instead.
// The getters and setters will access/alter the internal properties.
// We want this.
menu.meal = ...
menu.price = ...

// Note the absence of the leading underscores when using getters and setters.
// WITHIN the getters/setters, the actual properties (with underscore) _price 
// and _meal will be accessed/altered. But the names of the getters and 
// setters themselves won't have leading underscores.

By using getters/setters, we hide away the actual properties _price and _meal. As far as someone outside the object is concerned, they will use menu.meal and menu.price to access/alter the properties. The outside user doesn’t know and doesn’t care that the actual properties of the object are supposed to be menu._price and menu._meal.

The instructions specify,

…use the set keyword to create a meal setter method with mealToCheck as a parameter … In the body of the setter method, create an if statement that checks if mealToCheck is a string. If it is, return the object’s _meal property with mealToCheck assigned as the value.

With the above in mind,

// You wrote:
set _meal(mealToCheck) {
    if (typeof mealToCheck === 'string') {
        return this._meal = mealToCheck
    }
}

// It should be:
set meal(mealToCheck) {
    if (typeof mealToCheck === 'string') {
        return this._meal = mealToCheck
    }
}
// Note how the setter is meal instead of _meal.
// Within the setter, we are using _meal. But the
// setter's name is just meal.

Getters are supposed to return something, whereas setters usually just assign values to properties and don’t return the values. However, since the instructions of the project specify that the setter should return a value, so the use of return keyword is fine. Normally, the setter wouldn’t be expected to explicitly return.

In your other setter,

set _price(priceToCheck) {
    if (typeof priceToCheck === 'number') {
        return this.price = priceToCheck 
    }
}

there are a couple of problems. Firstly, the name of the setter should be price not _price. After you fix the setter’s name, you will run into the second problem. Within the body of the setter, you have written the statement this.price = priceToCheck. This is problematic. If you state setter is named price and the user makes a call to the setter, then the body of the state setter will be executed. this.price will make another call to the state setter and you will recurse ending up in an infinite loop till your program crashes as you run out of memory. Instead this._price = priceToCheck should be used within the body of the setter. With the above in mind,

set price(priceToCheck) {
    if (typeof priceToCheck === 'number') {
        return this._price = priceToCheck 
    }
}

should work.

Also,

// You wrote:
menu._meal = 'Pizza'
menu._price = 8

// Change it to:
menu.meal = 'Pizza'
menu.price = 8

As far as the user is concerned, the menu object has properties named meal and price. In actuality, that is not true as the actual properties are _meal and _price. But, these are internal details which are not meant to be exposed directly to the user. The actual names of the properties don’t concern the user.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.