-
Notifications
You must be signed in to change notification settings - Fork 0
/
lamp.js
51 lines (40 loc) · 1.19 KB
/
lamp.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
const keypress = require('keypress')
keypress(process.stdin)
const totalOfLamp = 10
let totalClickOfSwitch = 1
let lights = []
const switchLamp = () => {
if (totalOfLamp < 1) { return console.log(`Jumlah lampu harus lebih dari ${totalOfLamp}`) }
// initial lamp
if (lights.length < totalOfLamp) {
for (let i = 1; i < totalOfLamp + 1; i++) {
if (i % totalClickOfSwitch == 0) {
lights.push('off')
} else {
lights.push('on')
}
}
}
console.log("========================================")
console.log("Pencet sepasi untuk mengaktifkan saklar")
console.log(`Pencetan ke : ${totalClickOfSwitch}`)
lights.forEach((item, index) => {
if ((index + 1) % totalClickOfSwitch == 0) {
lights[index] = lights[index] == 'off' ? 'on' : 'off'
}
console.log(`${index + 1}. ${lights[index]}`)
})
console.log(`Total lampu hidup: ${lights.filter(i => {return i == 'on'}).length}`)
totalClickOfSwitch += 1
}
process.stdin.on('keypress', function (ch, key) {
if (key.name == 'space') {
switchLamp()
}
if (key && key.ctrl && key.name == 'c') {
process.stdin.pause();
}
})
process.stdin.setRawMode(true);
process.stdin.resume()
switchLamp()