-2
我對JavaScript很陌生,我可以使用一些幫助故障排除。 在控制檯日誌中顯示「upper」和「wordcount」沒有定義。此函數的目標是循環訪問inputdiv派生的數組,並詢問數組中是否存在數組值,AND不存在於「wordcount」數組中,如果不是,則將其推入。數組未定義,但我將它們定義爲變量
function processtext() {
var textindiv = document.getElementById("inputdiv").innerHTML;
var split = textindiv.split(" ");
var upper = [];
var wordcount = [];
for (var i = 0; i < split.length; ++i) {
upper.push(split[i].toUpperCase());
}
var sortedlist = upper.sort();
var wordcount = new Array;
for (var i = 0; i < sortedlist.length; ++i) {
if (sortedlist.indexOf(sortedlist[i]) > -1) {
if (wordcount.indexOf(sortedlist[i]) == -1) {
wordcount.push(sortedlist[i]);
}
}
}
}
console.log(upper);
console.log(wordcount);
[JavaScript中變量的範圍是什麼?](http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – undefined
順便說一下,'if (sortedlist.indexOf(sortedlist [i])> -1)'將始終爲'true';) – Robiseb