Return Largest Numbers in Arrays!

Return Largest Numbers in Arrays!
0

#1

Tell us what’s happening:

hi, freecodecamp says that my code is ok, but it seems different and more simple than other solutions. can someone check it?

Your code so far


function largestOfFour(arr) {

 let array=[];
 let a=0;
   for(let i=0;i<arr.length;i++){
       
        a= Math.max(...arr[i]);
       
        
       array.push(a);
       }
   
      return array; 
      
  
}

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 6.1; 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

Well done, this is an elegant solution! You avoid the need for an inner loop by making use of the spread operator (...) to unpack the current array (arr[i]) and feed all its values to the Math.max function.

You can simply it a little more by declaring the a variable inside the loop on one line with the Math.max function. You could even do away with the a variable altogether, and write the Math.max function directly into the push function, but this would make the code slightly less readable to others.


#3

thanks! i have almost a month that i started programming and this answer gives me courage to continue.
sorry if my English is bad


#4

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution.