1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/usr/bin/env node "use strict"; /** * * @param {number[]} nums * * @param {Function} fn * * @param {number} init * * @return {number} * */ var reduce = function (nums, fn, init) { let sum = init; for (var i = 0; i < nums.length; i++) { sum = fn(sum, nums[i]); } return sum; };