1. Show different alerts depending on if the circle is green or gray
To make this step work, we're going to need to use "conditional logic".
An "if-else" conditional logic statement has three parts:
- a condition, which is a test that is either true or false
- an "if" branch that holds code which runs if the condition is true
- an "else" branch that holds code which runs if the condition is false
It has the general structure of:
if (condition) {
// some code
} else {
// some code
}
Add an if-else statement to alert "turn off" if a circle has an "on" class and "turn on" if a circle doesn't have an "on" class.
document.addEventListener('click', function(event){
if (event.target.matches(".circle")){
if (event.target.classList.contains("on")){
alert("turn off");
} else {
alert("turn on");
}
event.target.classList.add("on");
}
});