Created
November 10, 2015 21:48
-
-
Save msonnabaum/f83ac59771ef7a219c40 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1,2,3].reduce(function(memo, current) { | |
console.log(memo); | |
}, {}); | |
// > {} | |
// > undefined | |
// > undefined |
When using reduce follow the 2 rules.
- Always pass an initialValue
- Always return the accumulator
var data = [1,2,3];
var initialValue = 0;
var reducer = function(accumulator, item) {
return accumulator + item;
};
var total = data.reduce(reducer, initialValue);
console.log('Sum of the array', data, 'is: ', total);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
memo is based on the return value which is empty, try