Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```javascript
- function reduce(array, callback, initialValue) {
- let accumulator = initialValue;
- for (let i = 0; i < array.length; i++) {
- accumulator = callback(accumulator, array[i]);
- }
- return accumulator;
- }
- // Uncomment these to check your work!
- const nums = [4, 1, 3];
- const add = function(a, b) { return a + b; }
- console.log(reduce(nums, add, 0)); // should log 8
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement