Last active
December 12, 2021 06:18
-
-
Save pH-7/f3bf982e2e13c9aa33392ec544e169c0 to your computer and use it in GitHub Desktop.
π¦ The simplest animal counter, using JS ES6's reduce() π
This file contains 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
const animals = [ | |
'Crocodile', | |
'giraffe', | |
'elephant', | |
'snake', | |
'elephant', | |
'giraffe', | |
'giraffe', | |
'chicken', | |
'crocodile' | |
]; | |
const animalCounter = animals.reduce((data, animal) => { | |
animal = animal.toLowerCase(); | |
data[animal] = (data[animal]) ? data[animal]+1 : 1; | |
return data; | |
}, {}); | |
console.log(animalCounter); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
whoa!