Console.log() not working on ES6: Use getters and setters to Control Access to an Object

Hi

Is there a reason console.log() is non-response - I can’t establish whether my code is right because I can’t get results from my console. Been trying all day and have got nothing, is there something obvious that I am missing?

function makeClass() {
  "use strict";
  /* Alter code below this line */
class Thermostat {
  constructor (farenheit) {
    this.farenheit = farenheit;
  }
  get temperature () {
    return 5/9 * (this.farenheit - 32);
  }
  set temperature (celsius) {
    this.temperature = celsius * 9. / 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

console.log(temp);

You might need to change the above line to the following one

set temperature (celsius) {
    this.farenheit = celsius * 9 / 5 + 32;
  }
1 Like

you have no this.temperature variable set in your constructor

when your object calls for it, it doesnt know where the variable is

2 Likes

Thanks for the ideas people, will give them a try in a bit

Ok, I see what’s going on.
Check this out instead

Convert Fahrenheit to Celcius - (spoiler alert)
class Thermostat {
  // creates the properties that the class will take
  constructor(fahrenheit) {
    this.fahrenheit = fahrenheit;
  }
  // makes a function to take input and convert to using formula
  toCelcius(fahrenheit) { // make method to convert your number
    let celcius = (fahrenheit - 32) * 5 / 9;
    return Math.round(celcius); // rounds to nearest integer
   // return celcius; // returns full number instead
  }
}

const temperature= new Thermostat() // create new object
let temp = temperature.toCelcius(76) // use the method created in your class object

console.log("temp = " + temp + "*C") //