So this is a task:
Basic Algorithm Scripting: 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]
.
I solved this with a kinda simple code:
function largestOfFour(arr) {
let largest = [(Math.max(…arr[0])), (Math.max(…arr[1])),(Math.max(…arr[2])),(Math.max(…arr[3]))];
return largest;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
But when I looked at other recommended solutions I saw none of the recommended solutions is anything similar to my code. Would this be considered a good practice in solving such problems or my code has a flaw? I understand that the real problem with my code is when we have more than 4 sub-arrays, but for this particular problem when we don’t have many sub-arrays is this a legit solution? Please take a note that I am a beginner and I am trying to understand JS as better as possible.
Thanks in advance for your answers.