Down below is a part of a program that I have issues with. Im getting a random word from a text-file thats separated by linebreaks and that separates the word in character arrays. Everything works fine within the function but it doesnt work on the global level. What do i do wrong? Thanks a bunch!
function getRandom(min, max) {
return Math.trunc(Math.random() * (max - min) + min);
}
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "database.txt", true);
txtFile.onreadystatechange = async function () {
if (txtFile.readyState === 4) {
// Makes sure the document is ready to parse.
if (txtFile.status === 200) {
// Makes sure it's found the file.
allText = txtFile.responseText;
lines = txtFile.responseText.split("\n"); // Will separate each line into an array
word = getRandom(1, lines.length);
letters = lines[word];
console.log(letters);
arrLetters = "";
arrLetters = letters.split("");
arrLetters.pop();
console.log(arrLetters);
}
}
};
txtFile.send(null);