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

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

#1

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.

``````
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");
``````

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`.

#2

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.

#3

How do you define “better”, “good”, “worse” solutions?

#4

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.

#5

#6

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)

“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.

#7

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

#8

Honestly, many of the solutions in the Guide are mislabeled in my opinion. Advanced does not always mean faster or uses less memory. Typically in the Guide, Advanced solutions typically use a new syntax/features found in ES6.

As far as time complexity and memory complexity none of the solutions in the guide are the most efficient or using the least amount of extra memory as is possible. For now, I would say what @hbar1st wrote above is good advice. I suggest you on’t get bogged down in trying to write code which is the fastest or uses the least memory, unless it actually becomes an actual issue in your job.

All but two of the FCC challenges can be solved without worrying too much about efficiency. The exceptions are the Sum All Primes and Smallest Common Multiple problems. With these, you must create an algorithm which will complete each test case in less time than FCC’s infinite loop protection will trigger the code to stop running.

Also, if you work on the Project Euler problems, efficient algorithms are an absolute must. In fact, for most of those problems you will need some strong math background to leverage certain mathematics principles to create efficient algorithms. A mere “brute-force” algorithm will not cut it for those problems.

The bottom line is, as long as you follow @hbar1st’s guidance above and a less efficient solution will work with the problem specifications, do not worry too much about it for now. You can always come back at a later time and refactor a solution as you learn more programming concepts and get better at developing algorithms.

#9

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).

#10

Most of the solutions in the Guide were from 2016, so at the time ES6 was considered advanced, since not as many people were familiar with it. It was an “advanced” solution. Several of the solutions in the Guide really need to be changed. I think the whole labeling of Basic, Intermediate, and Advanced should be removed and simply show the solutions with even more detailed comments explaining first the algorithm used and then the explanation of the code for the algorithm.