Created
October 17, 2021 18:53
-
-
Save lior-amsalem/2e938737f8541b2498cb07c84d565a2f to your computer and use it in GitHub Desktop.
sort names with & and commas
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
/** | |
list([ {name: 'Bart'}, {name: 'Lisa'}, {name: 'Maggie'} ]) | |
// returns 'Bart, Lisa & Maggie' | |
**/ | |
function list(names){ | |
let formatNames = ''; | |
names.map((a,b) => (b < names.length-2) ? formatNames += a.name + ', ' : ((b === names.length-2) ? (formatNames += a.name + ' & ') : (formatNames += a.name))) | |
return formatNames; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment