Learning JavaScript has been quite the adventure for me, but I can't seem to wrap my head around why I'm encountering an error on line 6 (specifically, 'cannot read property "length" of undefined) even though the length of each word in the index is printing correctly below it.
function findShort(s){
var input = s.split(' ');
var finalLength = 100;
for (var i = 0; i <= input.length; i++) {
if (input[i].length <= finalLength) {
console.log(input[i].length);
finalLength += input[i].length;
}
}
return finalLength;
}
findShort("this is a test");
As a result of this code snippet, I'll get:
// -> 4
// -> 2
// -> 1
// -> 4
// -> TypeError: Cannot read property 'length' of undefined
at findShort:6:16
at eval:14:1
at eval
at n.<anonymous>