Math.max -- Longestword algorithm problem

Math.max -- Longestword algorithm problem
0

#1

Tell us what’s happening:

I am trying to call Math.max in a .reduce to return the largest number out of an array. Calling ‘Math’ doesn’t light up, what could be the problem?

Your code so far

function findLongestWord(str) {
  var b= str.split(" ");
  var myArray= [];
  for (var i=0; i<b.length; i++) 
  
    {myArray.push( (b[i].length));}
myArray.reduce(function(acc, item) {
  return Math.max(acc, item);}, -Infinity);}
//return myArray;}


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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299.

Link to the challenge:


#2

The only thing missing from your solution is a return statement in front of the following:

myArray.reduce(function(acc, item) {
  return Math.max(acc, item);}, -Infinity)};

Helpful Suggestion: You should really make use of indentation to make your code more readable. See below where I have rewritten your solution with indentations. I also included the missing return statement.

function findLongestWord(str) {
  var b= str.split(" ");
  var myArray= [];
  for (var i=0; i<b.length; i++) {
    myArray.push(b[i].length);
  }
  return myArray.reduce(function(acc, item) {
    return Math.max(acc, item);
  }, -Infinity);
}