Why can't arrays be compared with === in Javascript? -
this question has answer here:
why isn't code correctly pushing words answer array? when change loop to:
(var k in testdict) { console.log(testdict[k].split("").sort()); }
it correctly prints i'm expecting, array of split, sorted characters words. i'm not sure why it's not evaluating equal , pushing answer. thanks!
function word_unscrambler(word, dict) { var testdict = dict; var answer = []; var word_scrambled = word.split("").sort(); (var k in testdict) { if (word_scrambled === testdict[k].split("").sort()) answer.push(testdict[k]); } console.log(answer); } word_unscrambler("kevin", ["trees", "but", "ankle", "nevik", "knive", "evin"]);
the problem testing arrays equality, since working strings, can join both arrays being compared:
function word_unscrambler(word, dict) { var testdict = dict; var answer = []; var word_scrambled = word.split("").sort().join(''); (var k in testdict) { if (word_scrambled === testdict[k].split("").sort().join('')) answer.push(testdict[k]); } console.log(answer); }
Comments
Post a Comment