BAS - the largest numbers in the array

Tell us what’s happening:
So I’m passing 3 out of 4 tests and I’m unsure how to pass the 4th one. Cause the 4th one has negative numbers in its output and I don’t know what I should change? I mean I’ve read on some posts that this line:

var newArr = [0,0,0,0];

could be the problem and I think it might be as well? I just don’t know how to adapt it to pass that last damn test? All help is welcome!

Thanks in advance!


Your code so far

function largestOfFour(arr) {
var newArr = [0,0,0,0];
for(let i = 0;i < arr.length;i++){
  for(let j = 0;j < arr[i].length;j++){
      if(arr[i][j] > newArr[i]){
        newArr[i] = arr[i][j];
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; rv:76.0) Gecko/20100101 Firefox/76.0.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

0 is too large of a number. You need a better initial guess for the largest value in each array.

1 Like

I just typed -100 into all the initial newArr values,lol! :laughing:
It works, thx for the nudge in that direction!

-100 will have the same problem with some arrays. To be really bulletproof, you should set them to a value that you know is in the array.