Tell us what’s happening:
I found this solution more explanatory till i got maxLength = words[i].length; My question is that how did maxLength discovered the longest word in the sentence and was able to return the value of 6 which is truely the length of the longest word “jumped”? If you remove the .length, i.e maxLength = words[i], and u log maxLength to the console, the console will return “The” which is a 3 letter word. I hope i am able to convey my message clearly.
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;
}
console.log(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/115.0.0.0 Safari/537.36
Challenge: Basic Algorithm Scripting - Find the Longest Word in a String
Link to the challenge: