-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-brick-and-break.js
More file actions
45 lines (39 loc) · 1016 Bytes
/
build-brick-and-break.js
File metadata and controls
45 lines (39 loc) · 1016 Bytes
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
// build the tower
export const build = (count) => {
let i = 1;
const interval = setInterval(() => {
if (i > count) {
clearInterval(interval);
return;
}
const brick = document.createElement("div");
brick.id = `brick-${i}`;
// determine column: 1-left, 2-middle, 3-right
const column = (i - 1) % 3;
if (column === 1) {
brick.dataset.foundation = "true"; // middle column
}
document.body.appendChild(brick);
i++;
}, 100);
};
// repair bricks
export const repair = (...ids) => {
ids.forEach((id) => {
const brick = document.getElementById(id);
if (!brick) return;
if (brick.dataset.foundation === "true") {
brick.dataset.repaired = "in progress";
} else {
brick.dataset.repaired = "true";
}
});
};
// destroy last brick
export const destroy = () => {
const bricks = document.querySelectorAll("div[id^='brick-']");
const lastBrick = bricks[bricks.length - 1];
if (lastBrick) {
lastBrick.remove();
}
};