Same but different:
const s = "Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill"; return s.toUpperCase().split(';') .map(name => name.split(':').reverse()) .sort() .reduce((accumulator, name) => ( accumulator += `(${name[0]}, ${name[1]})`), "");
You don't need the arbitrary join(':') after reverse() since sort() coerces the nested arrays to strings for you. That means you can avoid having to split again later.
join(':')
reverse()
sort()
But Amin is right:
const s = "Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill"; return s.toUpperCase().split(';') .map(name=> `(${name.split(':')[1]}, ${name.split(':')[0]}`) .sort() .join('');
That's way shorter! Thanks a lot for the input
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Same but different:
You don't need the arbitrary
join(':')
afterreverse()
sincesort()
coerces the nested arrays to strings for you. That means you can avoid having to split again later.But Amin is right:
That's way shorter! Thanks a lot for the input