This setter doesn't seem to be working

Hello, I’m doing the advanced objects lesson. I’ve walked through all the steps and it seems to be working but I wanted to test this setter by putting in a ‘false’ value so that it triggered the else statement. Now I can see that the setter doesn’t work, it is only taking the value from the actual property. What should I be doing different here to make the setter work?


const robot = {

  _model: '1E78V2',

  _energyLevel: 100,

  _numOfSensors: 15,

  get numOfSensors(){

    if(typeof this._numOfSensors === 'number'){

      return this._numOfSensors;

    } else {

      return 'Sensors are currently down.'



  set numOfSensors(num) {

    if (typeof num === 'number' && num >= 0) {this._numOfSensors = num} else {

      return 'Pass in a number that is greater than or equal to 0'




robot.numOfSensors = 'text';

console.log(robot.numOfSensors); // returns 15

Your setter works fine. You cannot set anything else than a number, that’s why it logs the value of _numOfSensors which was originally set. But since you just return your error message instead of logging it to the console, you don’t see the error message.

1 Like

Ok got it, that’s really helpful thanks!

1 Like