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