Return Largest Numbers in Arrays not passing the test

Tell us what’s happening:

can someone tell me what’s wrong in my code,I’m getting the largest number in the console though.

Your code so far


function largestOfFour(arr) {
  // You can do this!
    let greatest=0;
    for(let i=0; i<arr.length; i++){
        
    for(let j=0; j<arr[i].length; j++){
        
    if(arr[i][j]>greatest){
        greatest=arr[i][j];
    }
    }
        
    }
  return greatest;
}
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/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays

You are returning one number that is the greatest of all arrays. You need to turn one number for each subarray.

You are also not handling the case where the largest number of an array is less than zero

1 Like

Sorry I just realised, the question asks to return the array with the largest number. Anyway, i modified the code and now it returns the correct array in the console but still not passing the test.

the new code:


function largestOfFour(arr) {
  // You can do this!
    let greatest=0;
    let greatArr=[];
    for(let i=0; i<arr.length; i++){
        
    for(let j=0; j<arr[i].length; j++){
        
    if(arr[i][j]>greatest){
        greatest=arr[i][j];
        greatArr=arr[i];
    }
    }
        
    }
    
    
  return greatArr;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

That one just returns the largest numbers. It should return the largest number in EACH array. So if it’s 4 arrays, it should return one from each of those arrays, the largest ones.

Yours: [1000,1001,857,1] = All numbers in the last array.
Expected: [5, 27, 39, 1001] = Largest from Array 1, 2, 3, 4.

1 Like

Thank you, that did help,i modified the code… but if you don’t mind, can you pls check why, this doesn’t pass for arrays with -ve integers.

new code:

function largestOfFour(arr) {
  // You can do this!
 
var greatArr=[];
    for(var i=0; i<arr.length; i++){
           var largest=[i][0];
        for(var j=0; j<arr[i].length; j++){
            if(arr[i][j]>largest){
                largest=arr[i][j];   
            }
                
        }
   greatArr[i]=largest;
        
    }
    
    
  return greatArr;
    
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

It is for this, largest is being set equal to i. so with the 4th subarray, i is 3, and 3 is bigger than all the numbers… it is for luck that everything else passes

Or better, it is being set equal to the first element of an array containing only i

Thank you, solved it.

function largestOfFour(arr) {
var results = [];
for (var i = 0; i < arr.length; i++) {
var largestNumber = arr[i][0];
for (var j = 1; j < arr[i].length; j++) {
if (arr[i][j] > largestNumber) {
largestNumber = arr[i][j];
}
}


results[i] = largestNumber;
}


return results;
}


console.log(largestOfFour([[-25, -3, -80, -9], [13, 27, 18, 26], [32, 35, 37, 2000], [1000, 3001, 857, 1]]));