What do you think of this solution?

This was the challenge:

Return Largest Numbers in Arrays

Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.

Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i] .

Here is my solution:

function largestOfFour(arr) {
  let ans = []

  for(let i = 0; i < arr.length; i++){
    let x = arr[i]
    ans.push(x.reduce(function(long, index){return Math.max(long,index)}))
  return ans;

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

I am asking because after solving I checked the solutions provided and did not see this type of solution there. In fact, none of them used the reducer. Any critique, insight, feedback is what I am looking for.


Personally, I think mixing a raw loop and a functional method is a bit strange.

Would this be frowned upon on the clock?

Depends upon the team. Personally, yea, I’d request the code be revised to be all functional.

A loop is considered declarative? What is the best way to change the loops purpose here to a functional element

I would use a map here in place of the outer loop.

Got it thanks. I have not yet encountered map… onwards I go.