Find the Longest Word in a String; Question

Find the Longest Word in a String; Question
0

#1

This is what I came up with for the challenge. It seems to be right besides the “May the force be with you” sentence. The code does return the number 5, as it should, but it doesn’t count it as correct. Does anyone have any idea why?

var array1 = [];

function findLongestWord(str) {
var str1 = str.split(" ");

for (var i = 0; i < str1.length; i++) {
array1.push(str1[i].length);
}
var max = Math.max.apply(Math, array1);
return max;
}

findLongestWord(“May the force be with you”);


#2

Hi terabithian,
Get a hint:
try to define array1 as a local variable within a function. I’ve checked. It works.

Kind regards,
Ingvar.


#3

It does work. Thank you a ton!


#4