Given is an array which contains sub-arrays. The elements in this sub-arrays are numbers.
The task is to implement a JavaScript-function which returns an array with the smallest / largest numbers in the single arrays.
I have figured out this solution:
// Returns the smallest / largest
// numbers contained in a
// set of arrays.
// Parameter ---------------------
// 1.: Array - Containing other arrays.
// 2.: Function - Either Math.max or
// Math.min
// Return -------------------------
// Array - Containing numbers in case
// of success.
// Containing 'undefined' in case
// of elements which aren't arrays.
// Containing NaN in case of elements
// which aren't numbers.
function getExtrema(structure, funct) {
var ret = structure.map(function(value) {
if (Array.isArray(value)) {
return funct.apply(null, value);
}
});
return ret;
}
Complete code (with examples) on CodePen: http://codepen.io/mizech/pen/bEBMaW?editors=101
Are there better ways to solve the described task?