Build a Longest Word Finder App - Build a Longest Word Finder App

Tell us what’s happening:

Hey everyone! First time posting on here. I just got back into coding after about 10 years and am slowly making my way but on this one I’m completely stuck. As is stands now, my function returns the number 4 when it should return 6. How can I make sure that my longestWord variable doesn’t get overwritten at each iteration, but only when the next word is actually longer?
Help! :anxious_face_with_sweat:
Thank you,
Have a nice week and happy coding! :slight_smile:

Your code so far

function findLongestWordLength(sentence){
  let splitSentence = sentence.split(" ");
  let words = [];
  let separateWords = 0;
  for (let i=0; i < splitSentence.length; i++){
    separateWords = splitSentence[i].length;
    words.push(separateWords);
  }
  let longestWord = 0;
  for (let i=0; i < words.length; i++){
    if (words[i] < words[i+1]){
      longestWord = words[i+1];
    } else if (words[i] > words[i+1]){
      longestWord = words[i];
    }
    
  }
  return longestWord;
}
findLongestWordLength("The quick brown fox jumped over the lazy dog"));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 OPR/131.0.0.0

Challenge Information:

Build a Longest Word Finder App - Build a Longest Word Finder App

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-longest-word-in-a-string/a26cbbe9ad8655a977e1ceb5.md at main · freeCodeCamp/freeCodeCamp · GitHub

Welcome to the forum @MChance,

Here you are comparing the length of one word in the words array to the length of another word in that array rather than comparing the length of each word to the value of longestWord.

Happy coding

Oh ok I see it. I just completed the lab. Thank you so much dhess!

Happy coding!