I'm trying to write a text game where word loads from external file, a bar draws in from the left and the word comes from the right, when they collide a life will be lost. I am wondering why all code has to be in the 2nd if statement when loading the file. Why can't you just not make the file management as a function?
var wordNo, letters, lines, arrLetters;
function getRandom(min, max) {
return Math.trunc(Math.random() * (max - min) + min);
}
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "database.txt", true);
txtFile.onreadystatechange = function () {
if (txtFile.readyState === 4) {
if (txtFile.status === 200) {
allText = txtFile.responseText;
lines = txtFile.responseText.split("\n");
wordNo = getRandom(1, lines.length);
letters = lines[wordNo];
console.log(letters);
arrLetters = "";
arrLetters = letters.split("");
arrLetters.pop();
console.log(arrLetters);
}
}
};
txtFile.send(null);