Tell us what’s happening:
Can someone please explain the code, I’ve been stuck here for days and I still don’t understand what .split does and how the code runs.
Your code so far
function findLongestWordLength(str) {
let words = str.split(' ');
let maxLength = 0;
for (let i = 0; i < words.length; i++) {
if (words[i].length > maxLength) {
maxLength = words[i].length;
}
}
return maxLength;
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36
Challenge: Basic Algorithm Scripting - Find the Longest Word in a String
It is easier to explain by comparing it to your solution. If you don’t have a solution yet, i would back up and write one. Getting the checkmarks isn’t important - practicing coming up with solutions is the purpose of the challenges. We can help you fix your own solution if you are stuck with it.
It’s hard to come up with a solution when you don’t know what parts you should use to come up with one. To be more specific, I know how to get the length of the whole string but I don’t know how to get individual words of a string which is basically a whole sentence.