Created
April 22, 2017 09:16
-
-
Save amite/53a6b562d0d18b4f9dd49508090699bf to your computer and use it in GitHub Desktop.
Names statistics solution
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
``` | |
var names = ['Alice', 'Bob', 'Tiff', "Sonny", 'Bruce', 'Alice', "Sonny"]; | |
//output shoule be { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 } | |
var initialValue = {}; | |
var reducer = (names, stats) => { | |
(!names[stats]) ? names[stats] = 1 | |
: names[stats] = names[stats] + 1; | |
return names; | |
}; | |
var result = names.reduce(reducer, initialValue); | |
console.clear() | |
console.log(result); | |
``` | |
ES6 friendly version |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment