为什么这段代码不能正确地将单词推送到答案数组?当我将循环更改为:
for (var k in testDict) {
console.log(testDict[k].split("").sort());
}
它正确地打印出我所期望的,从单词中拆分、排序的字符数组。所以我不确定为什么它没有评估为相等并推动回答。谢谢!
function word_unscrambler(word, dict) {
var testDict = dict;
var answer = [];
var word_scrambled = word.split("").sort();
for (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"]);