Can't pass last test smoothly

Hi, so I’ve been doing this challenge and I actually passed it, but I don’t really like the way I did it.

Originally I reset the variable largestNumber to 0, but it didn’t pass the final test because 0 > -3 and it would put 0 in the array. So I reset largestNumber to -10, so it works now, but I don’t really like this method because if you were to input a number smaller than -10 it wouldn’t work.
How could I fix this while maintaining the same code structure?


function largestOfFour(arr) {
let a = [];
let largestNumber = 0;
for (let i = 0; i < arr.length; i++) {
  for (let j = 0; j < arr[i].length; j++){
    if (arr[i][j] > largestNumber) {
      largestNumber = arr[i][j];
    }
  }
  a.push(largestNumber);
  largestNumber = -10;
}

console.log(a);
return a;
}

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/92.0.4515.159 Safari/537.36

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

What about this one: on the first pass of that inner loop, what’s the largest number so far?

You’re right, if you choose an arbitrary value, every value in the array could be less than that.

But what if, before your inner loop, you set largestNumber to the first value in your sub-array? With that, you’re guaranteed to be in range.

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