Created
August 24, 2018 10:03
-
-
Save jido2015/1d865eb43bb6277c8fe2bdf9047cdd6e to your computer and use it in GitHub Desktop.
We have a characterScript function and a way to correctly loop over charac- ters. The next step is to count the characters that belong to each script. The following counting abstraction will be useful there:
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
| function countBy(items, groupName) { | |
| let counts = []; | |
| for (let item of items) { | |
| let name = groupName(item); | |
| let known = counts.findIndex(c => c.name == name); | |
| console.log(name); | |
| if (known == -1) { | |
| counts.push({name, count: 1}); | |
| } else { | |
| counts[known].count++; | |
| } | |
| } | |
| return counts; | |
| } | |
| console.log(countBy([1, 2, 3, 4, 5], n => n > 2)); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// → [{name: false, count: 2}, {name: true, count: 3}]The book says