Longest Word in a String JS Algo

Longest Word in a String JS Algo
0.0 0

#1

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/


#2

Click the Get a Hint button on the challenge and you will be taken to the Guide which gives other possible solutions to the problem.

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.


#4

This thread is not going to be a place where solutions are posted. Feel free to make comments about the OP’s code or give recommendations to the OP in the way of code snippet examples, but do not post any other full solutions. Thank you.


#5

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


#6

In general when you ask for help about your code, try to be specific about what part of your code you are concerned about and what exactly you are concerned about. There are a hundred ways to solve this problem. Some use more memory resources, but are fast. Some use no extra memory resources but are slower. Some are more readable, but not concise and others are concise, but not as readable.

I will give my 2 cents worth of commentary on making your existing code more concise while maintaining readability.

#1) Your if statement could be simplified to following, because the test method returns a true or false value and if statement conditions are evaluated as a Boolean value.

if (!check.test(str[i])) {

#2) The following else works, but to make it more readable, a more accurate indentation would make it clearer since you are not using { } to block your count = 0; statement.

else count = 0;
i++;

Some people would argue to always use the { } or at least put a single line to be executed on a an
separate indented line like:

else
  count = 0;
i++;

#3) To make your variable names better reflect what values they contain, I would rename the check variable to space and if you really wanted to go overboard with descriptive variable names, I would rename i to currStrIndex or currStrIdx. I would also rename count to be currWordLetterCount or currWordLen. It just makes it clearer exactly what you are counting. You can probably come up with something shorter if you spend a little time on it.

#4) I personally do not see the need for a ternary in the following line:

max = (count>max)?count:max;

because the following if statement would suffice, since max will just remain unchanged otherwise.

if (count > max) max = count;

#5) One last comment concerns the use of space. In your above ternary statement, you put no spaces between the right side of the =. This makes the code less readable in the name of making it more concise. A few extra spaces goes a long way in making your code more readable.

Again, these are the opinions of one person. Others may completely disagree with my comments or have several extra opinions to add on.


#7

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


#8

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.


#9

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.