improve randomizer

This commit is contained in:
zoe 2022-04-16 22:53:56 +02:00
parent d267fc4356
commit 3967fa1027

View file

@ -7,22 +7,29 @@ export function randomizeWords() {
function registerButtons() { function registerButtons() {
for (let button of buttons) { for (let button of buttons) {
button.addEventListener("click", function () { button.addEventListener("click", function () {
let new_word = processWordlist(button.getAttribute("data-wordlist")); button.innerHTML = processWordlist(
button.getAttribute("data-wordlist"),
button.innerHTML
);
// do this so people dont have to click twice // do this so people dont have to click twice
if (button.innerHTML == new_word) {
button.click();
console.log("oh no!!! this was already the value!");
} else {
button.innerHTML = new_word;
}
}); });
button.click(); button.click();
} }
} }
// takes all the words and returns only one // takes all the words and returns only one
function processWordlist(wordlist) { function processWordlist(wordlist, old) {
let seperated = wordlist.split(","); let seperated = wordlist.split(",");
for (let word of wordlist) {
word = word.trim();
}
if (seperated.length <= 0) {
return "error! empty";
}
if (seperated.length == 1) {
return seperated[0];
}
seperated = seperated.filter((e) => e !== old);
return seperated.random(); return seperated.random();
} }