Return Largest Numbers in Arrays new

Return Largest Numbers in Arrays new
0

#1

Tell us what’s happening:

i have gotten the highest number so far but the problem is getting an array from it and then which is what the problem demands.

Your code so far


function largestOfFour(arr) {
  let biggest;
  for (var i = 0; i < arr.length; i++){
    let biggest = arr[i].reduce((a, b) => {
      if (b > a){
        return b
      }
      else {
        return a
      }
       
    })
   console.log(biggest);
  }
  
  // You can do this!
 
return biggest;
}

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

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


#2

Looking good so far.

Since you need to return an array, why not initialize biggest as an empty array and then each time you find the biggest number in one of the sub-arrays, add that number to the biggest array.

As an additional challenge, since you seem to understand .reduce(), you could also implement this without any for loop at all by using a .map() and .reduce() together. After you successfully complete the challenge with a basic for loop, maybe try to do it again with .map() and .reduce()!