Skip to content

Instantly share code, notes, and snippets.

@lior-amsalem
Created October 17, 2021 18:53
Show Gist options
  • Save lior-amsalem/2e938737f8541b2498cb07c84d565a2f to your computer and use it in GitHub Desktop.
Save lior-amsalem/2e938737f8541b2498cb07c84d565a2f to your computer and use it in GitHub Desktop.
sort names with & and commas
/**
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