scrimba
Note at 1:39
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Note at 1:39
AboutCommentsNotes
Note at 1:39
Expand for more info
index.js
run
preview
console
/**
* 🎄 Challenge:
* 1. The Christmas tree's lights should switch
* on and off every 800 miliseconds.
*
* Stretch Goal:
* Make the blue and red lights flash alternately.
**/

function switchLights() {
setInterval(function() {
let redLights = document.getElementsByClassName("red")
let blueLights = document.getElementsByClassName("blue")

// turn on red lights
for (let i = 0; i < redLights.length; i++) {
redLights[i].style.filter = "none"
}

// turn off red lights after 1.5 seconds
setTimeout(function() {
for (let i = 0; i < redLights.length; i++) {
redLights[i].style.filter = "grayscale(100%)"
}
}, 1500)

// turn on blue lights after 1.5 seconds
setTimeout(function() {
for (let i = 0; i < blueLights.length; i++) {
blueLights[i].style.filter = "none"
}
}, 1500)

// turn off blue lights after 1.5 seconds
setTimeout(function() {
for (let i = 0; i < blueLights.length; i++) {
blueLights[i].style.filter = "grayscale(100%)"
}
}, 3000)
}, 3000) // total interval for switching lights
}

switchLights()
Console
/index.html
LIVE