const text_area = document.getElementById(
"text_area"
);
const choices_el = document.getElementById(
"choices"
);
const highlightChoice = (choice) => {
choice.classList.add(
"answer"
);
}
const unHighlightChoice = (choice) => {
choice.classList.remove(
"answer"
);
}
const pickRandomChoice = () => {
const choices = document
.querySelectorAll(
".choice"
);
return
choices[Math.floor(Math.random()
* choices.length)];
}
const createChoices = (input) => {
const choices = input
.split(
","
)
.filter((tag) => tag.trim() !==
""
)
.map((tag) => tag.trim());
choices_el.innerHTML =
""
;
choices.forEach((tag) => {
const temp = document.createElement(
"span"
);
temp.classList.add(
"choice"
);
temp.innerText = tag;
choices_el.append(temp);
});
return
choices.length;
}
const randomSelect = (choices) => {
const times = Number(choices) * 3;
const int = 150;
const interval = setInterval(() => {
const randomTag = pickRandomChoice();
highlightChoice(randomTag);
setTimeout(() => {
unHighlightChoice(randomTag);
}, int);
}, int);
setTimeout(() => {
clearInterval(interval);
setTimeout(() => {
const randomTag = pickRandomChoice();
highlightChoice(randomTag);
}, int);
}, int * times);
}
text_area.addEventListener(
"keyup"
, (event) => {
let choices = createChoices(event.target.value);
if
(event.key ===
"Enter"
) {
event.target.value =
""
;
randomSelect(choices);
}
});