I have a words.txt file that looks something like this:
account
arm
cotton
zoo
Using a XMLHttpRequest I am loading that file, creating an array list containing each line separately using the following code:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//this is where I split it
wordlist = this.responseText.split('\n');
}
};
xhttp.open('GET', 'words.txt', true);
xhttp.send();
Then I randomly pick one element from the list, let's say account
, and split it by ''
to get all individual characters:
word = wordlist[randomIndex].split('');
I expect the result to look like this:
["a","c","c","o","u","n","t"]
However, it turns out this way, having an additional empty string at the end:
["a","c","c","o","u","n","t",""]
How do I properly get rid of this?