Return Largest Numbers in Arrays.Help

Tell us what’s happening:
What is going wrong there?

Your code so far


function largestOfFour(arr) {
var answer = [];
  for (var i=0; i<arr.length; i++) {
    var biggestNumber = 0;
    for (var sl = 0; sl < arr[i].length; sl++){
      if(arr[i][sl] > biggestNumber) {
        biggestNumber = arr[i][sl];
      }
    }
    answer[i] = biggestNumber;
  }  
 // You can do this!
 return answer;
}

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/67.0.3396.99 Safari/537.36.

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

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

results[n] = largestNumber;

}
// You can do this!
return results;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

This is my new code.I belive it is right.

1 Like

Theres also another issue apart from the line

biggestNumber=0

you are adding the biggest number to your answer array at the index of i;
arrays are designed to add continuous values so instead of using the index to add the biggest element to answer array use the push method as

answer.push(biggestNumber);

here’s my simple solution to this problem

function largestOfFour(arr) {
let newarr=;
for(let i=0;i < arr.length ; i++){
let largest=arr[i].reduce((acc,val)=>{
if(val > acc){
acc = val;
}
return acc;
});
newarr.push(largest);
}
return newarr;
}

to get the most out of the free code camp challenges I recommend executing the challenges first in our local environment because this will help you think thoroughly over the problem and you can try your mistakes yourself by debugging.