How can I return the largest number in the lengthArr array?

Tell us what’s happening:

I don’t understand what the Math.max.apply() syntax is doing

Your code so far

function findLongestWordLength(str) {
var splitArr=[];
splitArr.push(str.split(" "));

for(var i=0; i<splitArr[0].length; i++){
  var lengthArr=[];
  var myIndex=splitArr[0][i].length;
  Math.max.apply(0, lengthArr);
return Math.max.apply(0, lengthArr);

console.log(findLongestWordLength("The in quick brown fox jumped over the lazy dog"));

Your browser information:

User Agent is: Mozilla/5.0 (Linux; U; Android 7.0; TECNO P701 Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/64.0.3282.137 Mobile Safari/537.36 OPR/52.1.2254.54298.

Challenge: Find the Longest Word in a String

Link to the challenge:

The function Math.max() takes multiple arguments in and returns the highest one.

Math.max(1,2,3,4,5); // returns 5

It will not, however, take an array in as an argument:

Math.max([1,2,3,4,5]); // return NaN

To solve this, you need to turn your array into multiple arguments. The method.apply() function takes in an array and turns it into multiple arguments, i.e.:

Math.max.apply(null, [1,2,3,4,5]) is equivalent to Math.max(1,2,3,4,5).

The first argument to the apply function supplies the this the object will see. Since that doesn’t matter unless you’re doing some object-oriented programming, passing in 0 or null is fine.

You can also use the spread operator the same way:

Math.max(...[1,2,3,4,5]); // returns 5

1 Like

You should read the docs as well.

Also keep in mind that this is one solution. If you haven’t learned the more advanced concepts described above, you can solve this problem using a loop.