Basic Algorithm Scripting - Return Largest Numbers in Arrays

Tell us what’s happening:
Describe your issue in detail here.
I solved this problem by setting maxValue to -100000 (instead of 0) so that it will work with negative numbers but is this best practice? Anyone know of a better way to get the if statement to be true for all real numbers?

  **Your code so far**
function largestOfFour(arr) {
let newArr = []
for (let i = 0; i < arr.length;i++){
  let maxValue = -1000000;
  for (let j = 0; j < arr[i].length; j++){
    if (maxValue < arr[i][j]){
      maxValue = arr[i][j];
    } 
  }
  newArr.push(maxValue);
}
console.log(newArr);
return newArr;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Return Largest Numbers in Arrays

Link to the challenge:

There are a few options:

  1. JS has the number Infinity which can also be negative.
  2. Set the maxValue to the first value of your data.
  3. Set it to null and when you check the number, if the maxValue is null then replace it.

I prefer one of the first two - less checking.

1 Like