Tell us what’s happening:
So I have spent a solid 20 minutes thinking this through and I really want to solve it without looking at the solution.
I get correct answers on this UNTIL i had the word “jumped…”
No idea whats happening. The answer I am getting for both function calls is 8
FOR CLARITY**
record is actually the highest number. I have since changed highCount to just count
When I use console.log(count) in the function, I get back that the function is seeing “brown fox” as an 8 letter word.
**Your code so far**
function findLongestWordLength(str) {
let highCount = 0;
let record = 0;
for (let i = 0; i <= str.length - 1; i++) {
if (str[i] !== " ") {
highCount += 1;
}
else if (str[i] === " " && highCount > record) {
record = highCount;
highCount = 0;
}
else if (str[i] === " " && highCount < record) {
highCount = 0;
}
}
console.log(record);
return record;
}
//im going to be really proud when I figure this out
findLongestWordLength("The quick brown fox jumped over the lazy dog");
findLongestWordLength("May the force be with you")
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36
Challenge: Basic Algorithm Scripting - Find the Longest Word in a String
Link to the challenge: