Find the Longest Word in a String - why doesn't return 19?

Hey everyone,

This code doesn’t work when used on the following string: “What if we try a super-long word such as otorhinolaryngology”. It should return 19 because this is the length of the longest word, but instead, it returns 4. I can’t figure out why is it happening. Please help me to understand, and hope you have a great day :slight_smile:

Tell us what’s happening:

Your code so far



function findLongestWordLength(str) {
var myLenght;
var numbers = [];
var i;
  var arr = str.split(" ");
for (i = 0; i < arr.length; i++) {
  myLenght = arr[i].length;
  numbers.push(myLenght);
}
numbers.sort();
  return numbers.slice(-1)[0];
  
}


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

It is because you are not using the sort function in such a way to correctly sort numbers. If you do not specify callback function for the sort, by default it sorts the elements as if they were strings. So your numbers array after the sort looks like:

[ 1, 10, 19, 2, 2, 2, 3, 4, 4, 4 ]

instead of

[ 1, 2, 2, 2, 3, 4, 4, 4, 10, 19 ]

You just got lucky on the other tests, because none of the words in the string were longer than 9 characters.

2 Likes

Your sort function is the problem. The default sort function doesn’t sort by increments.

Pass in this function

numbers.sort(function(a, b){
  return a-b;
});
1 Like

@shimphillip So the OP understands why you use the above function, can you explain how and why it works?

1 Like

Thank you for your help! I have read about the sort and compare functions, I think now understand how it works!

1 Like

javascript

let myStr = "What if we try a super-long word such as otorhinolaryngology";

function findLongestWordLength(str){
    let longestWord = str.split(" ").reduce(
        (x,y) => x.length > y.length ? x : y
    );
    return longestWord.length
}
console.log(findLongestWordLength(myStr));

That should give you 19.

I would advice against using sorting since sorting is an expensive operation being O(N*lg(N)).

This can be solved with a O(N) solution.

@danielsaldivarsalas That is great you figured out a different solution, but please try to answer the OP’s question and not just post a solution next time.

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 must post a full passing solution to a challenge, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

Thank you that was my first post. I did not realize we weren’t allowed to spoil solutions! I’ll make sure to use [spoiler] tags next time.

It is more about keeping your reply focused on answering the OP’s question. They did not ask for a new solution. They asked why their existing code does not return the value 19 for the one test.

But yes, we prefer campers to not just post a solution, but instead give the OP a hint or nudge in the right direction while trying to keep as much of their existing solution as possible.