Return Largest Numbers in Arrays: Which solution is better?

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

This is my solution for this execice.

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

As there are 4 solutions, I wonder which one is better ?

The one you understand, and that best describes itself. If i write a lovely elegant function that you don’t understand, what good is it?

From your perspective, which makes the most sense?

2 Likes

For me a short code (a minimized code) but easy to understand.

If your solution passes " Run the test " in Curriculum, it is not necessary to post it on the forum.

So here’s an approach to consider: purely as an experiment, can you write a function that finds the largest value in a simple array? Using that as a building block, can you then apply that function to each nested array in turn?

1 Like

Careful about ‘minimized’. Short is not always better. There is no benefit to making a function shorter, only in making a function clearer and simpler.

1 Like

I added spoiler tags around your code since it is a full working solution.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.