# Find the Longest Word in a String struggle

Find the Longest Word in a String struggle
0.0 0

#1

Hi guys,
I feel like I have the right logic but cannot make this work…
Can anyone help?

``````function findLongestWord(str) {
var array = str.split(" ");
array.sort(function (a,b){
return b-a;
});

return array[0].length;
}

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

Your Browser User Agent is: `Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36`.

#2

You should sort the array based on string length value instead of string itself.

#3

When you return b-a, you sort according to the alphabet, so you will need to adjust that to sort after length instead.

#4

Ok, so you decided to turn your string into an array of strings. So if we think about the data type that you are using, it might look something like this:

`[string, string, string, string]`

Then you decided to sort that array, so let’s have a look at the documentation for the `compare` function in the array’s `sort` function.

If compareFunction is supplied, the array elements are sorted according to the return value of the compare function. If a and b are two elements being compared, then:

If compareFunction(a, b) is less than 0, sort a to an index lower than b, i.e. a comes first.
If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.
If compareFunction(a, b) is greater than 0, sort b to an index lower than a, i.e. b comes first.
compareFunction(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned then the sort order is undefined.
(from MDN)

The important part here is that `a` and `b` are two elements from the array that are being compared.

So, the first thing to ask since `a` and `b` are both strings (i.e., words), what does it mean to say `return b-a`?

(I hope I am not being too obtuse. I don’t want to give away the answer. So please continue to ask questions if my hints are not enough!)

#5

Adding to what @garroadran mentioned, I modified your original code (see below) with a few console.log statements which you can run and see the results of the variables at different points in your solution. See my comments in the code.

``````function findLongestWord(str) {
var array = str.split(" ");
array.sort(function (a,b){
var bMinusA = b - a;
console.log(bMinusa); // bMinusA is NaN each iteration because you are trying to subtract a string from a string
return bMinusA; // returns NaN, so nothing gets changed in the array
});
console.log(array) // [ 'The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog' ]
return array[0].length; // the first word is 'The' and the length of 'The' is 3, so 3 gets returned
}

findLongestWord("The quick brown fox jumped over the lazy dog"); // 3
``````