Created
December 10, 2020 00:33
-
-
Save Gerst20051/f1163c724e92fbddd742479aaa8071d8 to your computer and use it in GitHub Desktop.
Dimmer Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIN_LEVEL = 5 | |
// MAX_LEVEL = 15 | |
// Light Switch | Brightnesses for given Bulb Capacity | |
// Dimmer Level | 5 Watt | 10 Watt | 20 Watt | |
// 5 | 0 | 0 | 0 | |
// 15 | 5 | 10 | 20 | |
// 10 | 2.5 | 5 | 10 | |
class Dimmer { | |
lights = []; | |
lightsMap = {}; | |
constructor(minBrightness, maxBrightness) { | |
this.minBrightness = minBrightness; | |
this.maxBrightness = maxBrightness; | |
} | |
setBrightness(brightness) { | |
if (brightness >= this.minBrightness && brightness <= this.maxBrightness) { | |
this.brightness = brightness; | |
} | |
// TODO: loop thru the lights and set brightness | |
} | |
addLight(light) { | |
if (!this.lights.find(_light => _light.id === light.id)) { | |
this.lights.push(light); | |
this.lightsMap[light.id] = light; | |
} | |
// TODO: set brightness on the light | |
} | |
removeLight(light) { | |
// this.lights = this.lights.filter(_light => _light.id !== light.id); | |
delete this.lightsMap[light.id]; | |
} | |
getBrightnessLevels() { | |
return Object.values(this.lightsMap).map(calcBrightness); | |
} | |
calcBrightness(light) { | |
return light.wattage * ((this.brightness - this.minBrightness) / (this.maxBrightness - this.minBrightness)); | |
} | |
} | |
class Light { | |
constructor(wattage) { | |
this.id = `${Math.random()}`; | |
this.wattage = wattage; | |
} | |
setBrightness(brightness) { | |
this.brightness = brightness; | |
} | |
} | |
const lights = [ | |
new Light(5), | |
new Light(10), | |
new Light(15), | |
]; | |
const dimmer = new Dimmer(5, 15); | |
dimmer.setBrightness(10); | |
for (let light of lights) { | |
dimmer.addLight(light); | |
} | |
dimmer.removeLight(lights[0]); | |
console.log(dimmer.getBrightnessLevels()); | |
console.log(JSON.stringify(dimmer.getBrightnessLevels()) === JSON.stringify([0, 0, 0])); | |
// console.log(JSON.stringify(dimmer(5, 15, 5, [5, 10, 20])) === JSON.stringify([0, 0, 0])); | |
// console.log(JSON.stringify(dimmer(5, 15, 10, [5, 10, 20])) === JSON.stringify([2.5, 5, 10])); | |
// console.log(JSON.stringify(dimmer(5, 15, 15, [5, 10, 20])) === JSON.stringify([5, 10, 20])); | |
// light = new Light(wattage) | |
// dimmer.addLight() | |
// dimmer.removeLight() | |
// dimmer.brighter() += 1 | |
// dimmer.darker() -= 1 | |
// dimmer.setMinBrightness() | |
// dimmer.setMaxBrightness() | |
// dimmer.setBrightness() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment