Find the Longest Word in a String Solution Why?

Tell us what’s happening:

This works, but I am not sure if it is basic/intermediate/ advanced solution or why! I figured it out myself.

I basically change the string into an array, sort the array into elements of in ascending order of length, shift the first element out because it has the highest length count, and then count the element length of the shifted element.

I know that works and it doesn’t use a lot of code.

I personally hate nested for or if switches so I try to do without.

Your code so far

function findLongestWord(str) {
  
var str1 = str.split(" ").sort(function(a, b){return b.length - a.length; });
  
  var longestWord = str1.shift().length;
  
   return longestWord;
  
  
}

findLongestWord("Google do a barrel roll");

//arr.sort(function (a, b) { return b.length - a.length; })[0];


//points.sort(function(a, b){return b-a});

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0.

Link to the challenge:
https://www.freecodecamp.org/challenges/find-the-longest-word-in-a-string

Using chained split then sort is not a beginner solution.
Your first variable str1 is referencing an array - not good naming

Creating and returning a variable would probably be more “beginner”

return str1[0].length;

would remove the need for the variable.

Thank you for the comment and the method to improve my code.

return str1[0].length would find the length of the first element in the newly created object array (from the initial string input).

I will rename the str1 variable into something more array oriented. I thought the str1 variable would be associated with the array.

I have blurred out your solution to avoid spoiling a full solution for other campers who happen upon your topic. 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.

1 Like

Thank you for assuming I didn’t know about spoiler tags because I didn’t. Honestly, it was the first time in a while that I came up with a solution on my own so I was eager to share.

In general, we try to discourage just sharing a passing solution for the sake of sharing it, because if everyone did the same, the forum would be overloaded with solutions. You can always use the Hint button on the challenge to see several other ways to solve a particular solution and compare yours to the others.

Keep up the good work!

Yes str1[0] is the same value as str1.shift() so,
str1[0].length
is the same as
str1.shift().length

Setting aside some of the comments above about naming, this is a perfectly clear, reasonable solution. Intermediate is a fine description.

It’s certainly not an ideal solution: sorting the array is doing more work than is truly necessary. For a thousand word text, sorting will compute b.length - a.length almost 7000 times. (ie O(n log n)) But in order to find the longest word, it’s enough to do only 1000 computations of b.length - a.length. (ie O(n))