Use getters and setters to Control Access to an Object 4246

<%= @topic_view.topic.title %>
<%= @topic_view.topic.average_rating %> <%= @topic_view.topic.posts.count { |p| !!p.custom_fields['rating'] } %>

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


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:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object

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.

Thanks for the explanation.:grin: