FAQ: Advanced Objects - Getters

This community-built FAQ covers the “Getters” 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 Getters

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!

4 posts were split to a new topic: Why don’t we need parenthesis for getters?

8 posts were split to a new topic: Double check the string if it all looks correct!

15 posts were split to a new topic: Why use a getter and not a method?

A post was merged into an existing topic: Double check the string if it all looks correct!

12 posts were split to a new topic: Understanding the use of typeof?

3 posts were split to a new topic: SCT doesn’t accept interpolation

2 posts were split to a new topic: SCT Expecting space

4 posts were split to a new topic: Why do I need to use console.log()?

For my own sanity can someone confirm this is correct as well (specifically my first return statement) Thanks!

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';
    }    
  }
};
1 Like

Hello, @andrew.allen. Welcome to the forums. Not sure what you mean here:

Your code doesn’t throw any errors. I can do this:

console.log(robot.energyLevel)

And get this output:

My current energy level is 100.

It seems a little odd to need to check whether the typeof this._energyLevel === 'number' in a getter. May be more appropriate in a setter. Are you having an issue?

Edit: I just looked at the exercise, and it does indeed require checking that the _energyLevel is a number in the getter. Your code appears to meet the requirements.

The text in the instructions does not include a full stop (a period) at the end. See what happens when you remove that.

Aside

There is no need to mix the type of quotes we use. Back-ticks may be used on all strings, not just those with interpolated expressions.

=== `number`

`System malfunction: cannot retrieve energy level`
2 Likes

Thank you both for your responses. mtf, you are correct. It was the full stop/period. Thanks!

1 Like

Wow. My issue was I put too many ; as I put them after each }

Thank you for this thread!