Basic Algorithm Scripting - Return Largest Numbers in Arrays

Tell us what’s happening:
Describe your issue in detail here.

trying to figure out how to get the largest number in the negative Array
function largestOfFour(arr) {
  let result = [];

  for (let i = 0; i < arr.length; i++) {
    let highestNum = 0;
    for (let j = 0; j < arr[i].length; j++) {
      let individualArray = arr[i][j];
      if (individualArray > highestNum ) { 
        highestNum = individualArray;
      }
    }
    result.push(highestNum)
  }
  return result;
}

console.log(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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Return Largest Numbers in Arrays

Link to the challenge:

Is there a reason you are setting the default to 0 when you know there can be negative numbers in the array?

You should change the highestNum value to the first value of the individual array so that elements inside the array are compared.

let highestNum = arr[i][0];

what if I made it -infinity would it work?

Try it out. Trying is free.

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