Why do I need to use console.log()?

Hi,

Why I can’t call the ‘get’ using only its own name as shown on the example person.fullName; // 'John Doe' and suggested by the step two: ‘Make sure you return the string and not logging it to the console.’ ?

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);

robot.energyLevel; //Why this alone is not enough to call the get? as shown on the lesson example?

What makes you say it isn’t called?
Helps if others can copy your code and run it, but you’ve lost a bunch of things when not formatting it properly in your post so nobody really knows what your code is, they’d have to do some guessing. There aren’t all that many buttons to choose from in the post editor, and there’s a preview. I believe in you.

1 Like
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)
// My current energy level is 100.

Since the method returns a string, we still need to log it or nothing happens.

Right… that is the answer for my mistake that I missed to understand, thank you…

Since the method returns a string, we still need to log it or nothing happens.

1 Like

Is there a reason it wants us to return the strings instead of console.logging them inside the if and else statements, because it works if I use console.log instead of return and then just call the getter method at the end instead of logging the result of calling it. The instructions want us to do this:

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);

But doing this also worked:

const robot = {
  _model: '1E78V2',
  _energyLevel: 100,
  get energyLevel(){
    if(typeof this._energyLevel === 'number') {
      console.log('My current energy level is ' + this._energyLevel);
    } else {
      console.log("System malfunction: cannot retrieve energy level");
    }
  }
};
robot.energyLevel;

I think worked is kind of the key term - in this quite narrow instance, how you’ve done it there gets the job done, but there are likely to be more situations in which you need the value to be returned from a function rather than logged. I think it’s just done like this to set you up with good practices for your future work (when you’re unlikely to be logging things to the console all that often, I suppose).

1 Like

In this case, they are functionally the same, but console logs don’t let you further manipulate, act on, or output the data in other forms, while returning data does.

Example: let’s say you built a javascript robot game and this object represents the player character. You want to display the energy level get text in the UI or perform certain tasks if "System malfunction: cannot retrieve energy level" is returned. By returning the string, you can either use it to console log stuff for debugging, or use the string as described above.

Of course, here you did not build a robot game, and don’t need the data for anything other than console logging, but it’s good practice.

1 Like