Longest Word in a String JS Algo

Code clears all test cases. I’d like to know if there are other approaches to it.
I’d also like to know your thoughts on my code.


function findLongestWordLength(str) {
  let check=/\s/;
  let i=0;
  let count=0;
  let max=0;
  while(i!==str.length){
    if(check.test(str[i])===false){
      count++;
      max=(count>max)?count:max;
    }
    else count=0;
      i++;
  }
  return max;
}

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/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/find-the-longest-word-in-a-string/

I’ll keep that in mind.
Anything about my code, though? Feedback?

If you’re after expressivity and leet style points this can be done essentially in a one line

You’ve done a really good strategy here though, O(n), single pass through the string

Another way is to split the string into words (this could maybe be done with split), find the lengths, find the max of lengths

With a higher order function this could look like text.split(...).map(...).reduce(max)

That has some disadvantages though, primarily creating a potentially long intermediate array of strings, though it’s very readable

Js really needs a lazier way to filter, map, and reduce combined in a single pass without handrolling it ourselves or wasting memory, I guess it’s hard when you can’t guarantee functions to be pure like in Haskell

Edit: of course they can all be combined into a reduce, but it hinders readability a lot

Thank you.
I guess if there’s not gonna be an else statement, I shouldn’t bother with ternary. A simple one line if does look good.

So you basically split it with " ", and we get an array of words. Using index we calculate lengths of each string element.
Nice!
If you’re willing, could you please complete your higher order function. I’d like to see how that works.