-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thermostat.js
54 lines (47 loc) · 1.27 KB
/
Thermostat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function Thermostat() {
this.temperature = 20;
this.minTemp = 10;
this.powerSavingMode = true;
this.setMaxTemp()
this.setEnergyUse()
};
Thermostat.prototype.upTemp = function(value) {
if ((this.temperature + value) > this.maxTemp ) {
throw new Error(`Cannot go above ${this.maxTemp} degrees!`)
};
this.temperature += value;
this.setEnergyUse()
};
Thermostat.prototype.downTemp = function(value) {
if ((this.temperature - value) < this.minTemp ) {
throw new Error(`Cannot go below ${this.minTemp} degrees!`)
};
this.temperature -= value;
this.setEnergyUse()
};
Thermostat.prototype.powerSavingSwitch = function() {
this.powerSavingMode = !this.powerSavingMode
this.setMaxTemp();
this.changeTemp();
};
Thermostat.prototype.setMaxTemp = function() {
this.maxTemp = this.powerSavingMode ? 25 : 32;
};
Thermostat.prototype.resetTemp = function() {
this.temperature = 20
this.setEnergyUse()
};
Thermostat.prototype.setEnergyUse = function () {
if (this.temperature < 18) {
this.energyUse = 'low-usage'
} else if (this.temperature < 25) {
this.energyUse = 'med-usage'
} else {
this.energyUse = 'high-usage'
}
};
Thermostat.prototype.changeTemp = function() {
if (this.temperature > this.maxTemp) {
this.temperature = this.maxTemp
};
};