DEV Community

loading...

Discussion on: Daily Challenge #66- Friend List

Collapse
blindfish3 profile image
Ben Calder

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.

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('');
Collapse
ynndvn profile image
La blatte

That's way shorter! Thanks a lot for the input