Last active
October 16, 2022 16:16
-
-
Save tawfiknasser/21d0735768cb13bde8e75d8c669004eb to your computer and use it in GitHub Desktop.
kata solution
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
function list(names){ | |
const arrayLen = names.length | |
return names.reduce((previousValue, currentValue, currentIndex)=>{ | |
if(currentIndex === arrayLen-1) | |
return previousValue+currentValue.name | |
if(currentIndex === arrayLen-2) | |
return previousValue+currentValue.name+' & ' | |
return previousValue+currentValue.name+','+' ' | |
},'') | |
} | |
function list(names) { | |
var xs = names.map(p => p.name) | |
var x = xs.pop() | |
return xs.length ? xs.join(", ") + " & " + x : x || "" | |
} | |
var list = (names) => names.map(x => x.name).join(', ').replace(/(.*),(.*)$/, "$1 &$2") | |
function list(names) { | |
return names.map(o => o.name).join(', ').replace(/^(.*)(, )(.*)$/, '$1 & $3'); | |
} | |
function list(names){ | |
return names | |
.map((item) => item.name) | |
.join(', ') | |
.replace(/,\s([^,]+)$/, ' & $1'); | |
} | |
function list(names){ | |
var last = names.pop() || { name: '' } | |
result = names.map(function(n) { return n.name }).join(', ') | |
return result ? result + ' & ' + last.name : last.name | |
} | |
function list(names){ | |
names = names.map(function(v){return v.name}); | |
return names.concat(names.splice(-2).join(' & ')).join(', '); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment