Skip to content

Instantly share code, notes, and snippets.

@pH-7
Last active December 12, 2021 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pH-7/f3bf982e2e13c9aa33392ec544e169c0 to your computer and use it in GitHub Desktop.
Save pH-7/f3bf982e2e13c9aa33392ec544e169c0 to your computer and use it in GitHub Desktop.
πŸ¦’ The simplest animal counter, using JS ES6's reduce() 🐘
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);
@Jkudjo
Copy link

Jkudjo commented Nov 30, 2021

whoa!

@pH-7
Copy link
Author

pH-7 commented Dec 12, 2021

whoa!

Hehe πŸ˜„, have you tried it @Jkudjo? πŸ™ƒ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment