Additional/Supplemental Info

Can anyone recommend additional info I can ingest alongside fCC J/s course. I feel a bit lost during the lessons. I find myself stumped, having to look at the hints on the smaller challenges. For example, using the Class keyword. I was able to get 90% of the information right but was completely lost on the structure of the Class/constructor object.

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

This was my code

// Only change code below this line
class Thermostat {
  constructor(temperature) {
    this.temperature = temperature;
  }

  //getter
  get temp() {
    return this.temperature = 5/9 * (thermos - 32);
  }

  //setter
  set temp(updatedTemperature) {
    this.temperature = updatedTemperature;
  }
}
// Only change code above this line

const thermos = new Thermostat(76); // Setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in Celsius
thermos.temperature = 26;
temp = thermos.temperature; // 26 in Celsius

This was the answer:

// Only change code below this line
class Thermostat {
  constructor(fahrenheit) {
    this.fahrenheit = fahrenheit;
  }
  
  get temperature() {
    return (5 / 9) * (this.fahrenheit - 32);
  }
  
  set temperature(celsius) {
    this.fahrenheit = (celsius * 9.0) / 5 + 32;
  }
}
// Only change code above this line

const thermos = new Thermostat(76); // Setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in Celsius
thermos.temperature = 26;
temp = thermos.temperature; // 26 in Celsius

I just feel like I’m not understanding it as I should.

How about this 1 hr tutorial?

1 Like

I’ll try this out. This part of the lesson was supposed to be an intro to OOP but I think I missed the grasp.

this is still not the answer
the setter and getter should be named temperature, you should not use a this.temperature property

Sorry I got my copy/paste wrong. I’ll update it.

you are using the class syntax perfectly fine — you may be having issues in algorithmic thinking, which is completely normal. Algorithms are an hard thing.

Yeah, it’s not an overnight understanding at all. I’m okay with that. This 1hr long video has way more information on the topic of classes and OOP.