Last active
January 17, 2024 16:36
-
-
Save AlbinSoft/4f3582d25d899e28a8077e644306c8a6 to your computer and use it in GitHub Desktop.
Join array strings in a human readable way
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
function strjoin(a) { | |
if(a.length===0) return ''; | |
let r = a.shift(); | |
while(a.length>1) { | |
r += ', '+a.shift(); | |
} | |
if(a.length) { | |
r += ' and '+a.shift(); | |
} | |
return r; | |
} | |
/* | |
'I like '+strjoin(['apples', 'bananas', 'cherries']); | |
I like apple, bananas and cherries | |
Demo: https://codepen.io/albinsoft/pen/jOJBxOJ?editors=0010 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment