DEV Community

Discussion on: Public Solving: Elf Coffee Shop menu

Collapse
 
lexlohr profile image
Alex Lohr

I find Array.prototype.reduce the ideal candidate for asymmetric input/output and it also allows us to forgo creating multiple arrays in memory, so the first part of my solution would look like this:

return drinks.reduce((flavored, drink) => {
  flavored.push({ drink: drink.name, flavor: undefined, price: drink.price });
  flavors.forEach((flavor) =>
    flavored.push({ drink: drink.name, flavor: flavor.name, price: drink.price + flavor.price })
  );
}, [])
Enter fullscreen mode Exit fullscreen mode

For the second part, I would utilize that 0 (equal sort value) it's coerced to false when using || and that string.prototype.localeCompare returns a number.

Collapse
 
dailydevtips1 profile image
Chris Bongers

Nice one!

For future puzzle i actually decided to rely a lot of reduce.
Must say this looks a bit cleaner.

Map made more sense if it was just the one array loop that we had to format.

Thanks for this wonderful alternative Alex 👏