Basic Algorithm Scripting - Return Largest Numbers in Arrays

Hi, I think my code actually works but it doesn’t pass the test.

let a=[];
let b=[];
function largestOfFour(arr) {
for(let i = 0; i < arr.length; i++){
  for(let j = 0; j <arr[i].length; j++){ 

    if(arr[i][j]<0){
      a.push(arr[i][j]);
      if(a[0]<arr[i][j]){
        a.shift();
      }else if(a[0]>arr[i][j]){
        a.pop();
      }
    }         
     
      if(arr[i][j]>a){ 
      a.shift();
      a.push(arr[i][j]);
      //console.log(a);
     }
  } 
  
    //console.log(a);
    b.push(a[0]);
  
  
    a=[];
  console.log(b); 
  
}
return b;

}


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 (X11; Ubuntu; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0

Challenge: Basic Algorithm Scripting - Return Largest Numbers in Arrays

Link to the challenge:

The code and the function works fine - when you run it once. But, if you run the function again and again, the results are not correct.

Ok, I haven’t try call the function more then once. Thanks!

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