Created
August 1, 2022 07:54
-
-
Save jimjam88/d6ddcf7a2b9762d1331aa4ad017c50ba to your computer and use it in GitHub Desktop.
Punctuation helper
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 punctuate = (items = []) => { | |
const amount = items.length; | |
return items.reduce((acc, item, index) => { | |
if (amount === 1) { | |
return item; | |
} | |
return index === (amount - 1) | |
? `${acc} and ${item}` | |
: `${acc}, ${item}`; | |
}, ''); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment