FAQ: Advanced Objects - Getters

Leave function body blank for now.

That would explain it. You’ve jumped ahead to the next step.

1 Like

In the Getters explanation codes, why the ternary expression doesn’t work (resulted ‘undefined’)?

For example:
… (codes start here)
get fullName() {
if (this._firstName && this._lastName){
return ${this._firstName} ${this._lastName};
} else {
return ‘Missing a first name or a last name.’;
}
}

Replacing with :
this._firstName && this._lastName ? ${this._firstName} ${this._lastName}: ‘Missing a first name or a last name.’;

person.fullName; // --> undefined

Sorry, my bad!
It should have been:
return (this._firstName && this._lastName)?${this._firstName} ${this._lastName}: ‘Missing a first name or a last name.’;

const robot = {

_model: ‘1E78V2’,

_energyLevel: 100,

get energyLevel() {

if (typeof this._energyLevel === isNaN){

  return `System malfunction: cannot retrieve energy level`; 

}else {

 return `My current energy level is ${this._energyLevel}`;

}

}

};

console.log(robot.energyLevel);

if I’m going to write my if statement like this and change the output like so.
Is this going to cause me a problem later on or I can do it like this??

There is no type isNaN. That is a global function to test for NaN.

The type we would want to check against is 'number'.

is there a reason why template literals were used in your first return statement Andrew?

Hi mtf,

can one use conditional operators in get statements ? if so what am I missing in below:

We cannot have statements of any kind inside an object unless they are inside a method. That means no let and definitely no, =.

I have checked this code and you are missing closing } curly bracket.

1 Like

This code looks right, but I can’t complete instruction two.

Did you use an if statement to check if this._energyLevel is a number and return the correct string inside the if statement?
const robot = {
  _model: '1E78V2',
  _energyLevel: 100,
  get energyLevel(){
    if (typeof this._energyLevel === 'number') {
      return 'My current energy level is ${this._energyLevel}';
    }
  }
};

This worked

const robot = {
  _model: '1E78V2',
  _energyLevel: 100,
  get energyLevel () {
    if(typeof this._energyLevel === 'number') {

    return 'My current energy level is ' + this._energyLevel

  }

}

}

If I delete the last semicolon, the error moves to line 77. There is nothing but commented out code there.

};
 ^
SyntaxError: Unexpected token ;
});
 ^
SyntaxError: Unexpected token )
const robot = {
  _model: '1E78V2',
  _energyLevel: 100,
  get energyLevel () {

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

    return 'My current energy level is ' + this._energyLevel
    } else {
      return `System malfunction: cannot retrieve energy level`
    }
};

EDIT:
The answer is a missing } on the end.

const robot = {
  _model: '1E78V2',
  _energyLevel: 100,
  get energyLevel () {

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

    return 'My current energy level is ' + this._energyLevel
    } else {
      return `System malfunction: cannot retrieve energy level`
    } console.log(robot.energyLevel)
}
}

This passes test 4 but I do not get anything logged to the console.

Have you tried writing the console.log statement after the final curly brace instead in the code block?

I can’t understand why we can’t remove the “get” and the method can still be successfully called on the object. Of course, when I remove it, it doesn’t work any more. But it was stated earlier that underscore doesn’t do anything in the code. It only lets other developers know to not mutate the property. So I fail to see why we can’t just call the method without having “get” in front of it.


if (typeof this._energyLevel === 'number')

When I use the typeof operator, how come .energyLevel can be classified as 'number', when I didn’t set anything equal to 'number'? Is the 'number' keyword preformatted for this lesson? Thanks.

No, it is actually a built in type. When we write,

console.log(typeof 42)

we get the description of its data type, 'number'.

1 Like

I had the same answer, and it works just as well, but the course itself will not pass to next step until the return is return 'My current energy level is written as:

return 'My current energy level is ' + this._energyLevel

Not sure why it will not accept is as

return `My current energy level is ${this._energyLevel}.`;

Did you see @mtf’s post after the one of mine you’re replying to? Your issue is likely the period at the end of your interpolated string. The SCT for the lesson isn’t expecting it.

return `My current energy level is ${this._energyLevel}.`;
//                             try removing the period ^

Also, welcome to the forums!