forked from Abrax-/morning-challenge-traffic-lights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
47 lines (39 loc) · 1.28 KB
/
script.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
// DON'T CHANGE ANYTHING BETWEEN HERE ---
var time = 1000;
var red = blinkLight(get('light-top'), 'red');
var yellow = blinkLight(get('light-middle'), 'yellow');
var green = blinkLight(get('light-bottom'), 'green');
var red2 = blinkLight(get('light-top2'), 'red');
var yellow2 = blinkLight(get('light-middle2'), 'yellow');
var green2 = blinkLight(get('light-bottom2'), 'green');
// blinkLight returns a function that switches a light on then waits a second
// before switching it off again and calling its first argument as a 'callback'.
// If no arguments are supplied the light just goes off.
function blinkLight(element, colour) {
var blink = function(callback) {
element.classList.add(colour);
setTimeout(function() {
element.classList.remove(colour);
if (callback) {
callback();
}
}, time);
};
return blink;
}
function get(id) {
return document.getElementById(id);
}
// --- AND HERE.
// Replace the example code below with your own solution:
function light() {
// HINT: when red() finishes it calls the first argument as a function.
// So in this example the flow is red() -> wait a second -> green()
red(green);
}
light();
function light2() {
// Same as above except the flow is green2() -> wait a second -> red2()
green2(red2);
}
light2();