-1

I'm just a beginner in JavaScript and I can't solve a problem with a function. I need the code to cycle through an array of let's say the numbers ["3", "5", "7", "4", "9"] and console.log only the highest number in the array. This is the code I'm working with and I had tried many variations to get the function to console.log the number 9 in this case. What is the proper way to insert an array here and I need to switch the return max to console.log(max). Until now I just had an error or the console wont show anything, I need an answer at this point to get me through it.

function findMax(arr){
  let max = arr[0];
  for(let i = 1; i < arr.length; i++){
    if(arr[i] > max){
      max = arr[i];
    }
  }
  return max;
}
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
Kas
  • 111
  • 3
  • 10

1 Answers1

-2
function findMax(arr){
  let max = arr[0];
  for(let i = 1; i < arr.length; i++){
    if(arr[i] > max){
      max = arr[i];
    }
  }
  return max;
}

console.log(findMax(['1','2','3','4','5']))
console.log(findMax([1,2,3,4,5]))