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';
    }    
  }
};
3 Likes

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.

2 Likes

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`
6 Likes

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

2 Likes

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

Thank you for this thread!

1 Like

On the Getters exercise at https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/learn-javascript-objects/lessons/advanced-objects/exercises/getters, my code keeps returning a syntax error. I’ve reset it and tried recoding a half dozen times, taking breaks to help avoid ‘lockdown’ syndrome, but nothing is working. Help please?

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

error

/home/ccuser/node_modules/babel-core/lib/transformation/file/index.js:558
throw err;
^

SyntaxError: /home/ccuser/workspace/advanced-objects-getters/main.js: Unexpected token, expected , (10:1)
e[0m e[90m 8 | e[39m e[36mreturne[39m e[32mSystem malfunction: cannot retrieve energy levele[39m
e[90m 9 | e[39m }
e[31me[1m>e[22me[39me[90m 10 | e[39m}e[33m;e[39m
e[90m | e[39m e[31me[1m^e[22me[39m
e[90m 11 | e[39m
e[90m 12 | e[39m
e[90m 13 | e[39me[0m
at Parser.pp$5.raise (/home/ccuser/node_modules/babylon/lib/index.js:4454:13)
at Parser.pp.unexpected (/home/ccuser/node_modules/babylon/lib/index.js:1761:8)
at Parser.pp.expect (/home/ccuser/node_modules/babylon/lib/index.js:1749:33)
at Parser.pp$3.parseObj (/home/ccuser/node_modules/babylon/lib/index.js:3978:12)
at Parser.pp$3.parseExprAtom (/home/ccuser/node_modules/babylon/lib/index.js:3719:19)
at Parser.parseExprAtom (/home/ccuser/node_modules/babylon/lib/index.js:7238:22)
at Parser.pp$3.parseExprSubscripts (/home/ccuser/node_modules/babylon/lib/index.js:3494:19)
at Parser.pp$3.parseMaybeUnary (/home/ccuser/node_modules/babylon/lib/index.js:3474:19)
at Parser.pp$3.parseExprOps (/home/ccuser/node_modules/babylon/lib/index.js:3404:19)
at Parser.pp$3.parseMaybeConditional (/home/ccuser/node_modules/babylon/lib/index.js:3381:19)
at Parser.pp$3.parseMaybeAssign (/home/ccuser/node_modules/babylon/lib/index.js:3344:19)
at Parser.parseMaybeAssign (/home/ccuser/node_modules/babylon/lib/index.js:6474:20)
at Parser.pp$1.parseVar (/home/ccuser/node_modules/babylon/lib/index.js:2340:24)
at Parser.pp$1.parseVarStatement (/home/ccuser/node_modules/babylon/lib/index.js:2169:8)
at Parser.pp$1.parseStatement (/home/ccuser/node_modules/babylon/lib/index.js:1861:19)
at Parser.parseStatement (/home/ccuser/node_modules/babylon/lib/index.js:5910:22)
at Parser.pp$1.parseBlockBody (/home/ccuser/node_modules/babylon/lib/index.js:2268:21)
at Parser.pp$1.parseTopLevel (/home/ccuser/node_modules/babylon/lib/index.js:1778:8)
at Parser.parse (/home/ccuser/node_modules/babylon/lib/index.js:1673:17)
at parse (/home/ccuser/node_modules/babylon/lib/index.js:7305:37)
at File.parse (/home/ccuser/node_modules/babel-core/lib/transformation/file/index.js:485:15)
at File.parseCode (/home/ccuser/node_modules/babel-core/lib/transformation/file/index.js:570:20)
at /home/ccuser/node_modules/babel-core/lib/transformation/pipeline.js:49:12
at File.wrap (/home/ccuser/node_modules/babel-core/lib/transformation/file/index.js:532:16)
at Pipeline.transform (/home/ccuser/node_modules/babel-core/lib/transformation/pipeline.js:47:17)
at Object.transformFileSync (/home/ccuser/node_modules/babel-core/lib/api/node.js:152:10)
at compile (/home/ccuser/node_modules/babel-register/lib/node.js:129:20)
at loader (/home/ccuser/node_modules/babel-register/lib/node.js:158:14)
at Object.require.extensions.(anonymous function) [as .js] (/home/ccuser/node_modules/babel-register/lib/node.js:168:7)
at Module.load (module.js:488:32)
at Object.load (/home/ccuser/node_modules/rewire/lib/moduleEnv.js:20:18)
at internalRewire (/home/ccuser/node_modules/rewire/lib/rewire.js:57:15)
at rewire (/home/ccuser/node_modules/rewire/lib/index.js:11:12)
at Object. (test.js:3:19)
at Module._compile (module.js:571:32)
at loader (/home/ccuser/node_modules/babel-register/lib/node.js:158:5)
at Object.require.extensions.(anonymous function) [as .js] (/home/ccuser/node_modules/babel-register/lib/node.js:168:7)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at /home/ccuser/node_modules/mocha/lib/mocha.js:220:27
at Array.forEach (native)
at Mocha.loadFiles (/home/ccuser/node_modules/mocha/lib/mocha.js:217:14)
at Mocha.run (/home/ccuser/node_modules/mocha/lib/mocha.js:469:10)
at Object. (/home/ccuser/node_modules/mocha/bin/_mocha:404:18)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3

What I don’t understand is, why asked us to use the operator “typeof” when we don’t need it as it tells me that it’s an error?? Without it, it lets me moved on fine…??? :face_with_monocle:

Ok, I’ve just re-visited by using

" typeof this._energyLevel === ‘number’ "

and it worked as where I’ve used

" this._energyLevel === 100 "

which also worked as well but I had the gut feeling that though it’s worked but something isn’t right as my gut feeling tells me, what if the number isn’t 100 and I truly just want to check if it was a number and not string or boolean…? I know we went through this but I’d prob’ forgotten.

Can someone please explain to me why or how does putting ‘number’ for the function to check to see if the property was a number works?? I feel like I’m missing or forgetting something…?

Basically, what I am trying to say is that the system knows that we want to check for number or string by simply using those words inside ‘’ right? Example, if we want to check if something has a value of number, do we literally just ask it by using (typeof object === ‘number’) or if it’s a string then we’d just (typeof object === ‘string’) is that right?

This is amazing!! I’ve discovered the answer myself and also I was curious on how to check what if I wanted to check if the property is valid while being either a ('string) or (‘number’) too!! :heart_eyes: :star_struck: :smiling_face_with_three_hearts: :smiling_face_with_three_hearts: :smiling_face_with_three_hearts: :smiling_face_with_three_hearts:

There are some fundamental errors which cause behavior that leads to a false positive. It traces to the logical expression used in your code.

this._firstName && this._lastName

When both names are present (their properties are set to a non-zero length string) this expression will yield, true. However, when we apply the expression to typeof we get "undefined" as the type.

 > typeof (this._firstName && this._lastName)
<- "undefined"

Notice that typeof returns a string object? That means it will pass your test, but for the wrong reason.

The test will return a true positive if we set the properties to numbers since number && number yields a number.

We can play with this some more to see what form of logic will ensure only true positives. First, give it some more thought run some more trials.

1 Like

Link:

why the first step is stating my code is wrong, pls?

const robot = {

  _model: '1E78V2',

  _energyLevel: 100,

  get energyLevel () {

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

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

  }

}

}