Find the Longest Word in a String: Solution not working

Find the Longest Word in a String: Solution not working
0

#1

I was wondering if someone could take a look at the code below and tell my why it won’t pass the tests. I wrote the same code on repl.it and it works just fine, but when on FCC I fail all the test sentences except “May the force be with you”

Your code so far

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

  var max = array[0];
  
  for (var i = 0; i < array.length; i++) {
  if (max.length < array[i].length) {
    max = array[i];
    return max.length;
  }   
    
  }
  
  
}

findLongestWord("The quick brown fox jumped over the lazy dog");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7.

Link to the challenge:


#2

I figured out my issue, I have my return statement in the wrong place. Should be as follows:

function findLongestWord(str) {
var array = str.split(" ");
var max = array[0];

for (var i = 0; i < array.length; i++) {
if (max.length < array[i].length) {
max = array[i];
}
}
return max.length;
}