Skip to content

Commit b4024b2

Browse files
committed
add random choice picker
1 parent 4932d23 commit b4024b2

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

Diff for: 13-random choice picker/index.html

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<link rel="stylesheet" href="style.css" />
7+
<title>Random Choice Picker</title>
8+
</head>
9+
<body>
10+
<div class="container">
11+
<h3>
12+
Enter all of the choices divided by a comma (',').<br />
13+
Press enter when you're done
14+
</h3>
15+
<textarea placeholder="Enter choices here..." id="textarea"></textarea>
16+
</div>
17+
<div id="tags"></div>
18+
<script src="script.js"></script>
19+
</body>
20+
</html>

Diff for: 13-random choice picker/script.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const tagsElements = document.getElementById("tags");
2+
const textarea = document.getElementById("textarea");
3+
4+
const createTags = (input) => {
5+
const tags = input
6+
.split(",")
7+
.filter((tag) => tag.trim() !== "")
8+
.map((tag) => tag.trim());
9+
tagsElements.innerHTML = "";
10+
tags.forEach((tag) => {
11+
const tagElement = document.createElement("span");
12+
tagElement.classList.add("tag");
13+
tagElement.innerText = tag;
14+
tagsElements.appendChild(tagElement);
15+
});
16+
};
17+
18+
const pickRandomTag = () => {
19+
const tags = document.querySelectorAll(".tag");
20+
return tags[Math.floor(Math.random() * tags.length)];
21+
};
22+
23+
const highlightTag = (tag) => tag.classList.add("highlight");
24+
25+
const unHighlightTag = (tag) => tag.classList.remove("highlight");
26+
27+
const randomSelect = () => {
28+
const times = 30;
29+
const interval = setInterval(() => {
30+
const randomTag = pickRandomTag();
31+
highlightTag(randomTag);
32+
setTimeout(() => {
33+
unHighlightTag(randomTag);
34+
}, 100);
35+
}, 100);
36+
37+
setTimeout(() => {
38+
clearInterval(interval);
39+
setTimeout(() => {
40+
const randomTag = pickRandomTag();
41+
highlightTag(randomTag);
42+
}, 100);
43+
}, times * 100);
44+
};
45+
46+
textarea.focus();
47+
textarea.addEventListener("keyup", (e) => {
48+
createTags(e.target.value);
49+
if (e.key === "Enter") {
50+
setTimeout(() => (e.target.value = ""), 10);
51+
randomSelect();
52+
}
53+
});

Diff for: 13-random choice picker/style.css

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
@import url("https://fonts.googleapis.com/css2?family=Muli&display=swap");
2+
3+
* {
4+
box-sizing: border-box;
5+
}
6+
7+
body {
8+
background-color: #2b88f0;
9+
font-family: "Muli", sans-serif;
10+
display: flex;
11+
flex-direction: column;
12+
align-items: center;
13+
justify-content: center;
14+
height: 100vh;
15+
overflow: hidden;
16+
margin: 0;
17+
}
18+
19+
h3 {
20+
color: #fff;
21+
margin: 10px 0 20px;
22+
text-align: center;
23+
}
24+
25+
.container {
26+
width: 500px;
27+
}
28+
29+
textarea {
30+
border: none;
31+
display: block;
32+
width: 100%;
33+
height: 100px;
34+
font-family: inherit;
35+
padding: 10px;
36+
margin: 0 0 20px;
37+
font-size: 16px;
38+
}
39+
40+
.tag {
41+
background-color: #f0932b;
42+
color: #fff;
43+
border-radius: 50px;
44+
padding: 10px 20px;
45+
margin: 0 5px 10px 0;
46+
font-size: 14px;
47+
display: inline-block;
48+
}
49+
50+
.tag.highlight {
51+
background-color: #273c75;
52+
}

0 commit comments

Comments
 (0)