32 lines
842 B
JavaScript
32 lines
842 B
JavaScript
|
const buttons = document.getElementsByClassName("randomword-button");
|
||
|
|
||
|
export function randomizeWords() {
|
||
|
registerButtons();
|
||
|
}
|
||
|
|
||
|
function registerButtons() {
|
||
|
for (let button of buttons) {
|
||
|
button.addEventListener("click", function () {
|
||
|
let new_word = processWordlist(button.getAttribute("data-wordlist"));
|
||
|
// 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();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// takes all the words and returns only one
|
||
|
function processWordlist(wordlist) {
|
||
|
let seperated = wordlist.split(",");
|
||
|
return seperated.random();
|
||
|
}
|
||
|
|
||
|
Array.prototype.random = function () {
|
||
|
return this[Math.floor(Math.random() * this.length)];
|
||
|
};
|