Return Largest Numbers in Arrays what is the wrong with my code?

Tell us what’s happening:
what is the wrong with my code?

Your code so far


function largestOfFour(arr) {
  let choosenArr;
  let largestNum = 0;
  for(let i = 0; i < arr.length; i++) {
    if(arr[i].reduce((a, b) => Math.max(a, b)) >= largestNum) {
      largestNum = arr[i].reduce((a, b) => Math.max(a, b));
      choosenArr = arr[i];
      
    }
  }
  return choosenArr;
}

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/70.0.3538.102 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 not grabbing the biggest numbers of each array and storing to chosenArr. You are just saving array subsets into chosenArr. In this case chosenArr will be just the last sub array.

1 Like

i thought that he need the array with the largest number