I'm trying to check a submitted string against a letterset. If word_string = "GAR", this should return "GAR" because these letters appear in letterset.
For some reason, some words appear correctly, and some appear with missing letters. For example, with word_string = "RAG", this returns "R". "FIG" returns "FG".
letterset = {0: "R", 1: "A", 2: "G", 3: "A", 4: "O", 5: "E", 6: "F", 7: "I"}
var ls = [];
for (prop in letterset) {
ls.push(letterset[prop]);
};
console.log(ls)
var word_string = '';
var word = document
.getElementById('word_container')
.childNodes;
for (var i in word) {
var w = word[i].innerHTML;
for (var prop=0; prop<ls.length; prop++) {
if (ls[prop] == w) {
console.log(w);
word_string += w;
ls.splice(prop);
}
}
}
lettersetan array? Why are you calling.splice()with only one argument, and have you thought about what happens if you remove elements from an array while you're iterating over that same array? What do you think that does to your for loop's indexing?