var arr = [1,4,5];
var getMaxOfArray = function(list, callback) { //this function return the max
if (typeof callback === "function")
return Math.max.apply(Math, callback.call(list));
else
return -1;
};
var cb = function(list) { //callback adds two to each value to a new array
var results = [];
for (var i=0; i<list.length; i++) {
results.push(list[i] + 2);
}
return results;
};
//cb(arr);
getMaxOfArray(arr, cb(arr));
When I call cb(arr), it evaluates as a function and returns the expected outcome of [3,6,7], but when I try to call getMaxOfArray(arr, cb(arr)); my conditional returns -1, showing that it is not a function? What am I doing wrong?
first, u need to call the getMaxOfArray method like this:
getMaxOfArray(arr, cb);
second, change the calling line to this:
return Math.max.apply(Math, callback.call(this, list));
or simply call the callback:
return Math.max.apply(Math, callback(list));