function reduce(arr, callback, val) { if(!Array.isArray(arr) || !arr.length || typeof callback !== 'function') { return [] } let rect = [] let value = val ? val : arr[0] let i = value ? 1 : 0 for (;i<arr.length;i++) { value = callback(arr[i], i, arr, value) } return value } function fn(curr, index, arr, value) { return curr + value } var arr = [...Array(5).keys()] console.log(reduce(arr,fn, 10))