question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ES6 - Use getters and setters to Control Access to an Object: Confusing?

See original GitHub issue

Describe your problem and - if possible - how to reproduce it

The exercise can pass with just one line of code.

  /* Alter code below this line */
  class Thermostat {}
  /* Alter code above this line */

It doesn’t test what it should be testing.

I think the instructions can be made clearer. This is what I have in mind:

Use the class keyword to create a Thermostat class. It should have a constructor that accepts a temperature in degrees Fahrenheit.

Now create a getter and a setter called temperature [or celsius, or tempCelsius, or temperatureC; they’re clearer, but changing to any of these needs changes in the last few lines of the code as well] in the class. The getter should return the temperature in degrees Celsius. The setter should accept a temperature in degrees Celsius.

Remember that °C = 5/9 * (°F - 32) and °F = °C * 9/5 + 32.

Note that while the constructor requires a temperature in Fahrenheit, you can store the temperature internally in either Fahrenheit or Celsius. What’s important is that the getter returns the temperature in Celsius and the setter accepts a temperature in Celsius.

This is the power of getters and setters - you are creating an API for another user, who would get the correct result, no matter how you implement them.

In other words, you are abstracting implementation details from the consumer.

Add a Link to the page with the problem

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object/

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:20 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
Starrzancommented, Jun 6, 2018

I encountered the same problem. The test passes without having to create the getter or setter, nor the constructor. The only code that is needed for the test to pass is the class declaration.

1reaction
IssamAthcommented, Oct 12, 2019

@ojeytonwilliams thanks i’ll give it a try !

Read more comments on GitHub >

github_iconTop Results From Across the Web

freeCodeCamp/use-getters-and-setters-to-control-access-to ...
You can obtain values from an object and set the value of a property within an object. These are classically called getters and...
Read more >
ES6 - Use getters and setters to Control Access to an Object
The constructor accepts Fahrenheit temperature. Now create getter and setter in the class, to obtain the temperature in Celsius scale. Remember ...
Read more >
Use getters and setters to Control Access to an Object
In the class, create a getter to obtain the temperature in Celsius and a setter to set the temperature in Celsius.
Read more >
Use getters and setters to Control Access to an Object
You can obtain values from an object and set the value of a property within an object. These are called getters and setters....
Read more >
Why do we use getters and setters in JavaScript? I ... - Quora
Getters and setters are just methods that are responsible for setting an instance variable (setter), and retrieving the value of an instance variable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found