-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraffic-light-sequencer.js
More file actions
70 lines (61 loc) · 1.26 KB
/
Copy pathtraffic-light-sequencer.js
File metadata and controls
70 lines (61 loc) · 1.26 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const config1 = {
fault: false,
phases: [
{ color: "green", duration: 5 },
{ color: "yellow", duration: 2 },
{ color: "red", duration: 4 }
]
};
const config2 = {
fault: false,
phases: [
{ color: "red", duration: 3 },
{ color: "yellow", duration: -2 },
{ color: "green", duration: 6 }
]
};
const config3 = {
fault: true,
phases: [
{ color: "green", duration: 5 },
{ color: "yellow", duration: 2 },
{ color: "red", duration: 6 }
]
};
const config4 = {
fault: false,
phases: []
};
function runSequence(config, cycles) {
if (config.fault) {
console.log("Faulted phase!");
} else if (config.phases.length === 0) {
console.log("No phases found")
}
else {
while (cycles--) {
for (let phase of config.phases) {
if (phase.duration >= 0) {
console.log(`Switching to ${phase.color} for ${phase.duration} s`)
} else {
console.log("Invalid phase detected")
}
}
}
}
}
function generateTimeline(config,cycles){
let array = [];
let total = 0;
if (config.phases.length === 0) {
return array
}
else {
while (cycles--) {
for (let phase of config.phases) {
array.push(total+=phase.duration)
}
}
}
return array
}