# Use getters and setters to Control Access to an Object 4246

**URL:** <https://forum.freecodecamp.org/t/use-getters-and-setters-to-control-access-to-an-object-4246/308801>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [August 24, 2019, 5:50pm UTC](https://forum.freecodecamp.org/t/use-getters-and-setters-to-control-access-to-an-object-4246/308801 "2019-08-24T17:50:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harshlebrown](https://avatars.discourse-cdn.com/v4/letter/h/ce73a5/32.png) [@Harshlebrown](https://forum.freecodecamp.org/u/Harshlebrown)\
**Post date:** [August 24, 2019, 5:50pm UTC](https://forum.freecodecamp.org/t/use-getters-and-setters-to-control-access-to-an-object-4246/308801/1 "2019-08-24T17:50:44Z")

</div>

**Tell us what’s happening:**

This code doesnt work if i dont declare var c,

is it necessary for a constructor parameter to work only if it is declared?

and does this code satisify the requirements of the question completely

**Your code so far**

```js

function makeClass() {
  "use strict";
  /* Alter code below this line */

    var c;

  class Thermostat{ constructor(c){

      
      this.c=c ;
  }

      set temperature (x){
        this.c=(5/9)*(c-32);
      }

      get temperature (){
        return this.c;
      }

  }
  
  /* Alter code above this line */
  return Thermostat;
}
const Thermostat = makeClass();
const thermos = new Thermostat(76); // setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in C
thermos.temperature = 26;
temp = thermos.temperature; // 26 in C

```

**Your browser information:**

User Agent is: `Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36`.

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![sandrab](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sandrab/32/118790_2.png) [@sandrab](https://forum.freecodecamp.org/u/sandrab)\
**Post date:** [August 25, 2019, 12:59pm UTC](https://forum.freecodecamp.org/t/use-getters-and-setters-to-control-access-to-an-object-4246/308801/2 "2019-08-25T12:59:33Z")

</div>

Look at your setter function, it uses a variable `c` that does not exist - unless you create a global variable `c` with the `var c;` statement at the top, but that is not what you intended. I think you intend to use the `x` function parameter of your setter instead. If you do that, the code no longer needs the global variabel at the top.

Secondly, to test if the code completely satisfies the requirements, you could add some statements to check the `temp` variable after you assign a value to it. For example, add something like `console.log(temp);` after both statements where you assign `thermos.temperature` to the `temp` variable. They should result in 24.44 and 26, like the code comments say.  
If you do that, I think you will find you need some adjustments in your getter and setter to get it right. Remember the class should take the temperature in Fahrenheit in its constructor (so you might not want to use `c` but `f` as a parameter and variable name, just to make it more clear). Consequently, because the getter and setter work with the temperature in Celcius instead of Fahrenheit, you should convert from Fahrenheit to Celcius in your getter and from Celcius to Fahrenheit in your setter.

---

<div class="post-metadata">

**Author:** ![Harshlebrown](https://avatars.discourse-cdn.com/v4/letter/h/ce73a5/32.png) [@Harshlebrown](https://forum.freecodecamp.org/u/Harshlebrown)\
**Post date:** [August 25, 2019, 4:13pm UTC](https://forum.freecodecamp.org/t/use-getters-and-setters-to-control-access-to-an-object-4246/308801/3 "2019-08-25T16:13:55Z")

</div>

Thanks for the explanation.😁

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 25, 2021, 12:19pm UTC](https://forum.freecodecamp.org/t/use-getters-and-setters-to-control-access-to-an-object-4246/308801/4 "2021-01-25T12:19:44Z")

</div>

Closed on January 25, 2021, 12:19pm UTC


