OOP air conditioner model I had a phone interview and we shared a screen to do a simple challenge. Instructions were ambiguous but I basically had to implement how an air conditioner would work in general. I wrote the following as I talked it out with the interviewer.

So I made a class with a parameter that would be a room temp and then added two properties, heatingSystem and coolingSystem and set them as booleans. Then wrote a function called setTemperature that should’ve been a while loop that changes the temperature and setting of the A/C until it reaches the set temperature.

class AirConditioner {
   constructor(temperature) {
       this.temperature = temperature;
       this.coolingSystem = false;
       this.heatingSystem = false;
   }



  temperatureSet(temperature) {
    while (this.temperature != temperature) {
       if (this.temperature &lt; temperature) {
           // turn on the cooling system and turn off heating system
           if (this.coolingSystem) {
               this.coolingSystem = false;
               this.heatingSystem = true;
               this.temperature++;
           } else {
             this.heatingSystem = true;
             console.log('Temperature has been lowered to ' + this.temperature);
             this.temperature++;
           }
       } else if (this.temperature &gt; temperature) {
           // turn off the cooling system and turning the heating
           if (this.heatingSystem) {
               this.coolingSystem = true;
               this.heatingSystem = false;
               console.log('Temperature has been increased to ' + this.temperature);
               this.temperature--;
           } else {
             this.coolSystem = true;
             console.log('Temperature has been lowered to ' + this.temperature);
             this.temperature--;
           }
       } else {
           // turn off cooling/heating system but keep the fan un
           console.log('temp is the same')
       }
    }
      console.log(`this.temperature: ${this.temperature} temperature: ${temperature}`)
  }
}

const AC = new AirConditioner(78);
console.log(AC);
AC.temperatureSet(63);
console.log(AC);


I honestly don't like the code and can already see a lot of possibilities on how to refactor or add other functions for re-usability. I was wondering what your suggestions are.
I don't know the position you were interviewed for. Below is the embedded engineer point of view.


A room temp is not a class parameter. The AC cannot control it directly, so this.temperature++ and this.temperature-- are incorrect. The AC may only read the temp sensor.
The desired temperature OTOH is a class parameter. You should go an extra mile and provide the interface to set it. BTW, I'd expect temperatureSet to do just that: set the desired temperature.
As implemented, the program terminates as soon as a certain temperature is reached. I expect an AC system to run more or less forever.
In the if (this.temperature &lt; temperature) clause you turn on heater no matter what. Move the this.heatingSystem = true; line outside of this.coolingSystem clause.


Assuming the above recommendations are implemented, the basic functionality should look like this:

    while(true) {
        ambientTemperature = this.temperatureSensor.read();
        if (ambientTemperature &lt; this.targetTemperature) {
            if (this.coolingSystem) {
                this.coolingSystem = false;
            } else {
                console.log('Temperature has been lowered to ' + ambientTemperature);
            }
            this.heatingSystem = true;
        } else {
        ....


Of course there is a plenty of room to improve, but again that depends on interviewer expectations, and the AC schematics you'd agree upon.