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! ![]()
Thank you,
Have a nice week and happy coding! ![]()
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