Hi there, I’m trying to solve this puzzle and I think im pretty much on the way. In my console the highest number is shown, so I would expect it to be fine when I return variable “highest”. But I get the following error:
undefined is not an object (evaluating 'array[i].length')
Whats going on here?
**Your code so far
function findLongestWordLength(str) {
let array = str.split(" ");
let arrayCount = [];
let highest = 0;
for (let i = 0; i <= array.length; i++) {
arrayCount.push(array[i].length);
highest = Math.max(...arrayCount);
console.log(highest);
}
return highest;
}
findLongestWordLength("The dsfad dasfdsffadefasdfedf fsd fsfdvcsrs");
It’s trying to tell you that array[i] is undefined, therefor it cannot treat it like an object, i.e., it cannot get a property on it. You can only try to access properties on objects (including arrays, functions, etc) and primitives that have associated object wrappers (string, number, boolean). But undefined and null are primitives without object wrappers so it will throw and error if you look for a property.
I’ll give you a hint - it’s not the first time that that is failing, but the last time. You need to examine your for loop, I think. You made a very common error with for loops.
In my code the outcome is 5, which is the expected answer, It says its not right.
let arrayCount = [];
let highest = 0;
function findLongestWordLength(str) {
let array = str.split(" ");
for (let i = 0; i < array.length; i++) {
arrayCount.push(array[i].length);
highest = Math.max(...arrayCount);
console.log(highest);
}
return highest;
}
findLongestWordLength("May the force be with you");
I couldn’t figure it out, so I decided to try something different.
let highest = 0;
let arrayOfWords = [];
let arrayOfNumbers;
function findLongestWordLength(str) {
arrayOfWords = str.split(' ');
for (let i = 0; i < arrayOfWords.length; i++ ){
if (arrayOfWords[i].length > highest) {
highest = arrayOfWords[i].length;
}
}
console.log(highest);
return highest;
}
findLongestWordLength("May the force be with you")
And to my surprise I have the exact same problem with this different code.
Also here my console gives the right answer, but the same test gives the same problem
That is what I/we we were trying to tell you. You still had the same problem with your new code because your variable highest was global. It needs to be local to your function. This makes your function reusable when your variables are local. The tests “reuse” your function by calling it repeatedly with different parameters. So when the function is called a second time highest is not 0, it it the number of the longest word the first time the function was called. If the longest word in the first call was 9, and the longest word in the second call is 6, your function returns 9. If the variable is local, highest is 0 before your for loop starts on the second call rather than 9, so now 6 would be higher than 0 and your function would return 6 on the second call. Also, your other variables should be local for best practices.
function findLongestWordLength(str) {
let stir = str.split(" ")
let sr = []
for (let st in stir){
sr.push(stir[st].length)
}
return Math.max(...sr);
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");