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.
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…???
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!!
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.