'Find the Longest Word in a String' HELP

Tell us what’s happening:
I did with ‘for’ loop and was trying other solutions on ‘Get a hint’.
It’s working when I give a initial value to reduce function but without it, giving me ‘NaN’. I thought if you don’t give the initial value it would take the first index as first value. isn’t it?

Your code so far


function findLongestWordLength(str) {
return str.split(' ')
.reduce(function(x, y){
  return Math.max(x.length, y.length);
});
}

console.log(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/80.0.3987.132 Safari/537.36.

Challenge: Find the Longest Word in a String

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/find-the-longest-word-in-a-string

1- turn string to an array
2- create a new sorted arrray using array.sort() (check a.length - b.length);
3- return the new sorted array first element(which is the longest one)

1 Like

I will explain why it is not working, but to going more in depth you will need to look at the documentation about reduce

so, you have an array of elements.
reduce takes a callback with two parameters, a current value and an accumulator. The accumulator can start with value of first element in the array, or you can put a second argument for the reduce method (the first argument is the function) and that second argument would be the start value of the accumulator. The other times the accumulator has value of output of previous callback call.

So, x is your current value, y is your accumulator.
The accumulator start with value "The", the current value has value of "quick". The output of this call is 5.
Second callback call, x has value of "brown", y has value of 5
Math.max("brown".length, (5).length)
that will give an error, as first, a number doesn’t have a length, and then he method try to compare a number and undefined.

note that if you don’t change a bit the function body, you will get this error even when giving a starting value

2 Likes

Oh! I see, now I can see where is wrong! thanks magical girl!! gonna fix the code right away and see if it’s working!

however it got me another curiosity, you said x is current value, y is accumulator, is it okay to swap their position?
I mean I was looking up about ‘reduce’ and they say first parameter is accumulator, second one is current value.

sorry, I was wrong - I didn’t check and went with memory.
Trust the documentation

(you can’t swap what the first and second paramenter do, you can however name them as you wish. I prefer to use acc and cur to remember what they do)

That’s okay, I’m really appreciate your explanation!
You don’t know how much I am relieved you are wrong :wink: otherwise I would spend much more time to get it. yeah I should name them acc, cur not to be confused.
Thanks :slight_smile: