Find the Longest Word in a String. Is this a 'good' solution?

Tell us what’s happening:
I came up with this solution to the challenge, but was curious about the answers given in the ‘Get a hint’ section, and to my chagrin saw that this wasn’t one of the given solutions. What I would like to know is, are the solutions given in the hints section better than mine? Though it passed the tests, is this (my solution) not a ‘good’ solution, or is it a ‘worse’ solution?
Also I noticed the recursive solution (in the hint section), recursion is still a little over my head, but is that a better solution since it’s more advanced? I know I’m asking a lot of questions here, with possibly subjective answers, but I’m very curious. Thank you.

Your code so far


function findLongestWordLength(str) {
  const longestWord = str.split(' ').map((x) => x.length).sort((a,b) => b-a);
  return longestWord[0];
}

findLongestWordLength("The quick brown fox jumped over the lazy dog");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) 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

considerations of memory allocation, breakability, readability, speed, and general conformation to established theory. Also just not seeing it in the hint section made me curious as to why those answers are there but not this one.

admittedly the answer involving reduce seems more elegant.

I find myself thinking along similar lines sometimes but in the end, it doesn’t matter so long as:

  • you aren’t repeating your code (sometimes code has lots of duplicate lines which is bad)
  • you have covered the test cases
  • you have covered edge cases that you can think of
  • your code is readable (imagine coming back after one year, will you know what your code was doing?)
  • your code is maintainable ( you haven’t coded in so many assumptions that if the question changes slightly, everything must be re-written in order to accommodate a design change)

Eg. you may ask, how easy is it to adapt this code to answering:
“find shortest word length”
What if the requirement changes to ‘return the longest word’.
If you think changing the code to accommodate these would require a full re-write, then your code is not ‘the best’.

hope this helps.

good advice. Will keep those things in mind, especially thinking about how flexible the code is in what it can do if the desired outcome were to change.
Thank you

1 Like

I was wondering about that (the mislabeling) because I sometimes felt the cleaner solution was the best when reading the hint section and I couldn’t figure out from the guidelines what was expected of the various types. (eg. if I wrote the answer in less lines of code, does that make it more advanced. What if it is harder to read too?) But it is good to know that the ES6 usage seems to be the differentiation so I will look out for that next time).