-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (46 loc) · 1.15 KB
/
index.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
48
$(window).on("load", function () {
function start() {
// initialize
$("#scale").on("change", () => start());
$("table").remove();
// construct field
const fieldSize = +$("#scale").val();
const $body = $("body");
const $table = $("<table>");
$body.append($table);
[...Array(fieldSize)].forEach(() => $table.append("<tr>"));
const $trs = $("tr");
$trs.each((_, tr) =>
[...Array(fieldSize)].forEach(() => $(tr).append("<td>"))
);
const cells = $("td");
console.log(`scale: ${cells.length}`);
let touched = 0;
function touch(self) {
self.off("click");
self.removeClass("selected");
touched += 1;
$("#counter").html(`${touched}`);
loop();
}
let prev = -1;
function next() {
while (true) {
const idx = Math.floor(Math.random() * cells.length);
if (prev !== idx) {
prev = idx;
return idx;
}
}
}
function loop() {
const idx = next();
const target = $(cells[idx]).addClass("selected");
target.on("click", function () {
touch(target);
});
}
loop();
}
start();
});