Return largest number in array


#1

//Hi I’m trying to figure out what’s wrong with my code when I’m trying to return the largest number in an array
//Example. findMax[0,200,1,5,3] should return 200

function findMax(n){
  var max = 0;
  for (var i = 0; i < n.length; i++){
    if (n[i] > max){
      var max = n[i];
    }
  }
  return max;
}

#2

The variable is declared before the loop so should not use a var keyword inside the loop.

findMax[0,200,1,5,3] should return 200

should be,

findMax([0,200,1,5,3])

#3

Thank you so much for your help!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.