Basic Algorithm Scripting - Return Largest Numbers in Arrays

Tell us what’s happening:

Hi everyone,

I was wondering how I can handle negative numbers with unknown value in the arrays. Do I add an insanely high amount of zeros to my maxNum = -100?
Or what do I need to do?
I thought about using Math.min( ) as value for maxNum, but this will log infinity for every number in the final array.
What am I missing?

Looking forward to your help! :blush:

Your code so far

function largestOfFour(arr) {

let arrMax = [];

for (let i = 0; i < arr.length; i++){
  let maxNum = -100;
  for(let j = 0; j < arr[i].length; j++){
    if(arr[i][j] > maxNum){
      maxNum = arr[i][j];
    }
  }
  arrMax.push(maxNum);
  console.log(arrMax)
}
  return arrMax;
}

largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Challenge Information:

Basic Algorithm Scripting - Return Largest Numbers in Arrays

I’d try “guessing” a number you know is in the array instead of a “big” negative number.

I don’t get what you are trying to say

Why pick -100? You have no way of knowing if -100 is smaller than everything in the array.

But here you are looking at all the elements in the array. So why not pick one as your first maxNum guess?

1 Like

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