Created
January 12, 2026 15:38
-
-
Save artlung/c4f26b7a8766a54fe5187d6b3534c6f4 to your computer and use it in GitHub Desktop.
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
| /** | |
| * Given an array of objects representing bears in a forest, each with a | |
| * name and hunger level, return the names of all bears whose hunger | |
| * level is above the forest average, sorted alphabetically. | |
| * | |
| * @param bears{name: string, hunger: number}[] | |
| * @returns {string[]} | |
| */ | |
| function hungryBears(bears) { | |
| // Was happy to reacquaint myself with [],reduce and "".localeCompare | |
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | |
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare | |
| const averageHunger = bears.reduce((sum, bear) => sum + bear.hunger, 0) / bears.length; | |
| return bears | |
| .filter(bear => bear.hunger > averageHunger) | |
| .map(bear => bear.name) | |
| .sort((a, b) => a.localeCompare(b)); | |
| } | |
| /* from RENDEZVOUS WITH CASSIDOO ~ Interview question of the week | |
| https://buttondown.com/cassidoo/archive/the-future-is-sending-back-good-wishes-and/ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment