arrays - odd results from nested for loops javascript -
i'm trying check submitted string against letterset. if word_string = "gar"
, should return "gar"
because these letters appear in letterset.
for reason, words appear correctly, , appear missing letters. example, word_string = "rag"
, returns "r"
. "fig"
returns "fg"
.
letterset = {0: "r", 1: "a", 2: "g", 3: "a", 4: "o", 5: "e", 6: "f", 7: "i"} var ls = []; (prop in letterset) { ls.push(letterset[prop]); }; console.log(ls) var word_string = ''; var word = document .getelementbyid('word_container') .childnodes; (var in word) { var w = word[i].innerhtml; (var prop=0; prop<ls.length; prop++) { if (ls[prop] == w) { console.log(w); word_string += w; ls.splice(prop); } } }
i think using splice incorrectly, regardless bit on complicated try:
(var in word) { var w = word[i].innerhtml; if (ls.indexof(w)> -1) { word_string += w; } }
Comments
Post a Comment