0

I was working through a problem on Codewars and saw the following solution as being the most clever as voted on by the community:

const dataReverse = a => a.join``.match(/\d{8}/g).reverse().join``.split``.map(Number);

I was wondering how this person invokes the .join, and .split methods without using any parentheses.

The incoming data is one long array whose contents is originally numbers (0 or 1) and length will always be a multiple of 8.

If more information is needed, please let me know. Thank you!

user2796352
  • 974
  • 3
  • 14
  • 23
  • 2
    They're abusing template literal tags. Template literals are strings delimited by backticks instead of quotes, and they let you interpolate values in them and include line breaks. They also let you "tag" them by prepending a function before the opening backtick, which passes the parts of the literal into the function to let you customize how it's interpolated. In this case, the literal is an empty string, so `a.join`, etc. get joined with no delimiter, just as if thye'd called `a.join()` normally. – IceMetalPunk Jul 18 '19 at 17:36
  • @IceMetalPunk Thank you :) – user2796352 Jul 18 '19 at 17:38
  • 1
    @IceMetalPunk Actually it's calling `a.join([""])`, passing in an array with an empty string, not "no delimiter". *Fortunately* `join` casts its argument to a string, which for this array yields the empty string. – Bergi Jul 18 '19 at 19:02

0 Answers0