Company name asks you to build a website with 7 boxes shaped like a c, which should turn green on click, and then when all are clicked, should turn red in reverse clicking order.
https://jsfiddle.net/yx296/dvacLduw/
document.addEventListener('DOMContentLoaded', function() {
var boxHolder = document.querySelector(".boxHolder");
var count = 0;
boxHolder.addEventListener('click', changeGreen);
var boxes = [];
function changeGreen(e) {
if (e.target !== e.currentTarget) {
var box = e.target;
if (box.style.backgroundColor !== 'green') {
box.style.backgroundColor = 'green';
count++;
boxes.push(e.target);
}
}
if (count === 7) {
count = 0;
setTimeout(function() {
reverseTurnRed();
}, 1000);
}
e.stopPropagation();
}
function reverseTurnRed() {
boxes.reverse();
boxes.forEach(function(box, index) {
setTimeout(function() {
box.style.backgroundColor = 'red';
}, index * 500)
});
setTimeout(function() {
boxes = [];
}, 0)
}
});
div {
box-sizing: border-box;
}
.container {
font-size: 0;
margin: 0 auto;
width: 400px;
height: 400px;
border: 1px solid blue;
position: relative;
}
.boxHolder {
border: 1px solid black;
display: inline-block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.box {
width: 50px;
height: 50px;
border: 1px solid black;
margin: 2px;
}
.topRow {
display: inline-block;
}
.bottomRow {
display: inline-block;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vanillaJSAddeparChallenge</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<div class="boxHolder">
<div class="topRow box"></div>
<div class="topRow box"></div>
<div class="topRow box"></div>
<div class="box"></div>
<div class="bottomRow box"></div>
<div class="bottomRow box"></div>
<div class="bottomRow box"></div>
</div>
</div>
<script src="practice.js"></script>
</body>
</html>
It seems it doesn't matter whether or not I leave the e.stopPropagation
in there... what is the point of it?
Willing to accept any critique on HTML, CSS or JavaScript.