FAQ: Advanced Objects - Setters

This community-built FAQ covers the “Setters” exercise from the lesson “Advanced Objects”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Setters

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

8 posts were split to a new topic: Don’t we need to check _numOfSensors instead?

I’m stuck on step two, I’ve added the setter, and the console is not giving me any errors. However, the system is telling me I’m still making a mistake, can someone please advise?

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;
    }
  }
};

the line after your if statement…this.__numOfSensors instead of this._numOfSensors

2 Likes

When I complete the final task on this exercise console.log(robot.numOfSensors);, I note that if you assign robot.numOfSensors as a string instead of a number, it logs the expected message followed by the previous _numOfSensors value:

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

Is this correct? I understand why _numOfSensors is unchanged, but I don’t understand the code is returning it.

This line uses the getter to return _numOfSensors.

1 Like

Ah. So the variable num that’s passed in the setter is invalidated by the setter as it’s not a number, however the variable _numOfSensors is already present and correct for the getter to reference, so it returns console.log from both the getter and the setter (whereas if the setter passed the if statement as true, it would only reassign the _numOfSensors variable and nothing would be logged to the console.) Think I’ve got it. Thanks!

1 Like

There is a thing, that I dont understand:

if the codeline reads:
robot.numOfSensors = 100;
the output ist: 100

if the codeline reads:
robot.numOfSensors = “something”;
the output is: Pass in a number that is greater than or equal to 0

but if the codeline is:
robot.numOfSensors = 100a;
it produces an error (SyntaxError: Unexpected identifier)

My question is, how does javascript consider “100a”, if it is not in brackets that mark it as a string? I have the feeling, that I did not understand some basic conception.

Anything that is not in quotes is regarded as an identifier. 100 is a number, but a is not. JS sees this as an unexpected identifier.

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 = ‘dfdd’

console.log(robot.numOfSensors)

HUH ?

Why does this return 15 ?

should it not return : Pass in a number that is greater than or equal to 0

Because the value remains unchanged due to incorrect inputs. The setter should have raised the error message. We don’t need to test inside the getter, just return the value.

get numSensors () {
    return this._numSensors;
}

I don’t understand. If its incorrect inputs why is it not then returning the else value ?

should it not return either : ‘Sensors are currently down.’ from the getter or ‘Pass in a number that is greater than or equal to 0’ from the setter?

The error message should behave as expected in the setter. There is no need for any message in the getter. Just return the value.

If you wish to return a message from the getter when the value is 0,

return this._numSensors ? this._numSensors : "Sensors are down";

Zero is falsy to will revert to the default in the third expression of the ternary.

Correct :smiley:

Yes…the setter wouldn’t log anything to the console, but instead the getter now logs the reassigned value (100) when we call it with console.log(robot.numOfSensors)

Thanks for that example using the string '100' instead of the number. I worked it through and it helps to see and understand what the getter and setter are doing :sweat_smile: :muscle:

It does, but it will only log to the console if you change the setter’s else statement from…

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

to…

console.log('Pass in a number that is greater than or equal to 0')

console.log(robot.numOfSensors) calls the getter (NOT the setter) so this prints 15 to the console, because the 15 remains unchanged (the setter fails the reassignment condition).

robot.numOfSensors = 'dfdd' calls the setter, but when the string fails the reassignment condition, the message is only logged to the console if you console.log it within the setter, rather than just return it.

@mtf

1 Like

Hello, I still do not get what the use of getters and setters as I am able to produce the same result without it.

const robot = {
_model: ‘1E78V2’,
_energyLevel: 100,
_numOfSensors: 15,
numOfSensors(){
if(typeof this._numOfSensors === ‘number’){
return this._numOfSensors;
} else {
return ‘Sensors are currently down.’
}
},
numOfSensors(num){
if (typeof num === ‘number’ && num >= 0) {
this._numOfSensors = num;
}else {
console.log(‘Pass in a number that is greater than or equal to 0’)
}
}

};

robot.numOfSensors = 100;
console.log(robot.numOfSensors) // still logs 100

hence, if anyone can explain a bit clearer that would help. Thanks!

Did you refresh the browser and then run it again?

> const robot = {
     _model: '1E78V2',
     _energyLevel: 100,
     _numOfSensors: 15,
     numOfSensors(){
       if(typeof this._numOfSensors === 'number'){
         return this._numOfSensors;
       } else {
         return 'Sensors are currently down.'
       }
     },
     numOfSensors(num){
       if (typeof num === 'number' && num >= 0) {
         this._numOfSensors = num;
       } else {
         console.log('Pass in a number that is greater than or equal to 0')
       }
     }
   }
<- undefined
 > robot.numOfSensors
<- ƒ numOfSensors(num){
       if (typeof num === 'number' && num >= 0) {
         this._numOfSensors = num;
       } else {
         console.log('Pass in a number that is greater than or equal to 0')
       }
     }

Notice what is logging? The second numOfSensors method, which means the first one was overwritten.

When you ran this line,

robot.numOfSensors = 100;

what effectively happened is you overwrote that method with a new property.

console.log(robot.numOfSensors) // 100
console.log(robot._numOfSensors) // 15
> robot.numOfSensors = 100
<- 100
 > robot.numOfSensors
<- 100
> robot._numOfSensors
<- 15
 > robot
<- {_model: "1E78V2", _energyLevel: 100, _numOfSensors: 15, numOfSensors: 100}

Notice that now neither method is present.

oh okay, I understand, thanks for your help!:smiley:

1 Like

const robot = {
_model: ‘1E78V2’,
_energyLevel: 100,
_numOfSensors: 15,
set numOfSensors(num){
if(typeof num === ‘number’ && num >= 0){
return this._numOfSensors = num;
} else {
console.log(‘Pass in a number that is greater than or equal to 0’)
}
},

};
robot._numOfSensors = 100
console.log(robot.numOfSensors)
Can someone explian me 100 is a number but why it shows undefined.