i'm having trouble understanding where my logic fails! When I try to impose a word.length condition on pushing the word i've got from a paragraph in an array, i get stuck in an infinite loop. Please let me know your thoughts, thanks to all!
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;
}
iis not part of an expression, so pre-incrementing it or post-incrementing it doesn't matter.