So I came up with a solution that I haven’t seen elsewhere for this problem and thought I’d share it, and if I’m wrong and it is elsewhere someone can correct me.
function largestOfFour(arr) {
for (let subarr in arr) {
arr.splice(subarr, 1, Math.max(...arr[subarr]));
}
return arr;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Edit: I hadn’t thought about how splice would cause the indexes to reorganize after each one, thanks @camperextraordinaire! Here is my updated code after @luishendrix92 tip about the .map() method which I hadn’t heard of before.
function largestOfFour(arr) {
var n = 0;
arr.map(function(group){
arr[n++] = Math.max(...group);
});
return arr;
}
let result = largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
(Last?) Edit: Thank you @ilenia for pointing out that I forgot to return the value from the inner function, and @JivkoJelev91 for inspiring me to do some more research on arrow functions. I learned a good bit from this site. This solution by @JivkoJelev91 is definitely my favorite:
return arr.map(grp => Math.max(...grp));