function each(collection, func) {
if (Array.isArray(collection)) {
for (var i = 0; i < collection.length; i++) {
func(collection[i], i);
}
} else {
for (var key in collection) {
func(collection[key], key);
}
}
}
function map(array, func) {
var result = [];
each(array, function(element, i) {
result.push(func(element, i));
});
return result;
}
function findMax(numbers) {
var maximum = numbers[0];
each(numbers,function(x){
if(x>maximum){
maximum = x;}
});
return maximum;
}
function maxNumbers(arrays){
return map(arrays, function(x){
return findMax(arrays);
})
}
maxNumbers([1,2,3],[5,6,7])
I'm struggling to understand the concept of the map function. I successfully created a function to find the maximum number in an array using the each function, and now I'm trying to apply that to the map function. However, the return statement returns the same maximum number three times for each array, which I don't quite understand. I've tried different approaches like returning max(arrays[x]) but it didn't work as expected. Any help would be appreciated.