Find most long element

Find most long element
0

#1

Tell us what’s happening:

Now I turn the string to array, after this i should check for length each element for length, but how I can find most long element among them? Is it by var max = Math.max.apply(null, arr);
?
Your code so far


function findLongestWordLength(str) {
  let arr = str.split(" ");
 

  console.log(max);
  return str.length;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 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


#2

Think about your algorithm. What steps would you take to figure out which word in the sentence is the longest?

One way, is to assume the longest word has a length of 0, which could be stored in a variable at the start of your code. Next, you would iterate through the array of words you have created (arr) and check the current word’s length to your variable which contains the longest word length value. If the current word’s length is greater than the “longest length” variable, then assign this new found length to the variable, otherwise do nothing and move to the next word. Once you have iterated through the entire array of words, your “longest length” variable should contain the value of the longest worth length in the sentence, which you can return.


#3

Ofcourse, math.max() would be helpful.
You will have to find out yourself how you would use it.
I would recommend playing with Math.max() in your browser console.
Try to understand how it works with arrays.

Hope this helps.