Last active
August 29, 2015 14:07
-
-
Save amundo/f8b4755f8fc46e1e82b0 to your computer and use it in GitHub Desktop.
counting things in javascript
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 count = function (sequence) { | |
| return sequence.reduce(function (tally, item) { | |
| if (!(item in tally)) { | |
| tally[item] = 0 | |
| } | |
| tally[item] += 1; | |
| return tally | |
| }, { | |
| }) | |
| } | |
| var words = 'pease porridge hot pease porridge cold pease porridge in the pot nine days old'.split(' '); | |
| console.log(count(words)) | |
| //{ pease: 3, porridge: 3, hot: 1, cold: 1, in: 1, the: 1, pot: 1, nine: 1, days: 1, old: 1 } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Brendan pointed out:
This is because
Object.prototypehas a method called.watch()in Firefox. Lame. So you can do this:Which works, but then you write over
.watch(), which is deprecated anyway. But still. Lame.