我无法理解我的逻辑在哪里失败!当我尝试对从数组中的段落中获得的单词施加 word.length 条件时,我陷入了无限循环。请告诉我你的想法,谢谢大家!
var str = document.getElementsByTagName('p')[0].innerHTML;
console.log(str);
function wordIndexes(str) {
var result = [];
var len = str.length;
var i = 0, j, word;
while (i < len) {
if (str[i] === ' ') {
++i;
}
else {
word = "";
for (j = i; j < len && str[j] !== ' '; ++j) {
word += str[j];
}
console.log(word.length);
//imposing length conditions
if (word.length < 4)
{console.log('too short')}
else {
result.push([i, word]);
i = j;
};
}
}
return result;
}