I have this code. and for some reason i get the error: Uncaught TypeError: Cannot read property 'length' of undefined. I can't seem to figure out why.
function findShort(s) {
var c = s.split(' ');
var l = c[0].length;
var wordlength;
for (var i = 1; c.length; i++) {
console.log(c[i].length);
wordlength = c[i].length;
if (l < wordlength) {
l = wordlength;
}
}
return l;
}
console.log(findShort("hello dog cat 12 asdsad wuidhuiwqhd"));
the error is with the line wordlength = c[i].length;
When i console.log it, it shows the length of each string in the array. However, when i try to store that length it says it's undefined.
c.lengthdoesn't change during the loop. So you keep going past the end of the array.console.log(Math.min.apply(null, 'the quick brown fox jumped'.split(' ').map(w=>w.length))); // outputs "3"