HELP in Use getters and setters to Control Access to an Object

HELP in Use getters and setters to Control Access to an Object
0

#1

Tell us what’s happening:

I’ve been approaching this with an if/else logic in the code; meaning that the get is the format that I begin the code with (if metaphor) and set is the code that mutates it (else). Anyway, that’s not important but that’s my reasoning.

That is the reason that I have put the conversion formula in the set as it seemed from what I’d read in the forum posts and in the challenge that fahrenheit was a default. I’m not passing any tests so I’m definitely doing something wrong.

What am I missing?

Your code so far


function makeClass() {
  "use strict";
  /* Alter code below this line */
class Thermostat {
  constructor() {
    this.temperature = temperature;

    get temperature(){
      return this.temperature;
    }
  }
    set temperature(celsius){
      this.temperature = (temperature-32)*5/9;
    }

}
  /* 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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 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


#2
  1. You need to have the temperature argument in constructor(arg) like this and the constructor is used to set the Fahrenheit temperature!
    2 . get temperature () should be outside of the constructor()
  2. get temperature() should return temperature in celsius scale , so you need to covert the Fahrenheit scale to celsius scale inside this method
  3. set temperature() should also set the temperature in Fahrenheit scale, so you need to convert the celsius scale to Fahrenheit scale inside this method

#3

Here’s my code. I followed your suggestions. I’m not passing any tests and the = on my get code has a red error mark under it, which I’m perplexed by. It all seems to be following according to the logic of the code. The constructor sets up the F setting, the get returns a celsius return and set modifies the Celsius back to Fahrenheit.

Where am I going wrong?


function makeClass() {
  "use strict";
  /* Alter code below this line */
constructortemperature(){
  this._temperature=temperature;
}
get (temperature());{
  return this._temperature-32(5/9) = temperature;
}
set (updateTemperature(C*9.0/5+32));{
  this._temperature = (updateTemperature * 9.0/5+32);
}
  /* 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

#4

constructor() , get() and set() should be inside a class ,
like this

/* Alter code below this line */
class Thermostat{
    constructor(ftemperature){
      this.ftemperature = ftemperature;
    }
    get temperature(){
     //write your logic here to covert  Fahrenheit to Celsius. Like this 
     let celsiusTemperature =  5 / 9 * (this.ftemperature - 32);
     return celsiusTemperature;
    }
    set temperature(celsius){
    //write your logic here to covert Celsius to Fahrenheit.
    this.ftemperature = celsius * 9.0 / 5 + 32;
    }
  }
  /* Alter code above this line */
  return Thermostat;
}

Refer the above structure of the code ,
Below is a simple class syntax

class className {
    constructor() { },
    get() { },
    set() { },
}

I think you didn’t understood the syntax of a class and functions correctly ,
There are some 3 to 4 mistakes in your code all are related to syntax,
Refresh your knowledge on functions , how to pass a value to a function,
Hope this helps :smiley:


#5

Thanks a lot. I went back in reviewed it alongside your comments and it made more sense.

Obliged.