Hello fellow Campers,
I am working on the fifth task of the Basic Algorithm Scripting - Chapter and cannot seem to figure out why I keep getting an error.
This is my solution which produces the following error “TypeError: Cannot read property ‘length’ of undefined.”
What is it that I miss?
function findLongestWord(str) {
var array = [];
var stringcontainer = ‘’;
var stringbuffer = ‘’;
var arraylen = 0;
array = str.split(’ '); // splitting string into an array
arraylen = array.length; // determining length of array
//comparing each array element to find longest one
for (var a=0; a<=arraylen; a++){
stringbuffer = array[a];
if (stringcontainer.length < stringbuffer.length)
stringcontainer = array[a];
}
return stringcontainer.length;
}
findLongestWord(“The quick brown fox jumped over the lazy dog”);