6
\$\begingroup\$

I'm new to ES6 and code-golfing. Here's the original function:

function puzzle(n, x) {
  return n * Math.pow(2, x);
}

And here's what I have so far:

let puzzle=(n,x)=>n*2<<(x-1)

But the solution requires lesser characters. I went through the 'Code Golfing with ES6' thread, but couldn't find a better way. Any help/links would be appreciated.

\$\endgroup\$
3
  • 6
    \$\begingroup\$ You can try n<<x. \$\endgroup\$
    – Leaky Nun
    Commented Aug 21, 2016 at 14:11
  • 1
    \$\begingroup\$ FYI there isn't much point in a Stack Snippet if you aren't going to build it into some HTML \$\endgroup\$
    – Beta Decay
    Commented Aug 21, 2016 at 14:39
  • \$\begingroup\$ Ah. That did it :) @βετѧΛєҫαγ Point noted. \$\endgroup\$
    – Avi Jain
    Commented Aug 21, 2016 at 14:41

1 Answer 1

11
\$\begingroup\$

17 bytes:

puzzle=n=>x=>n<<x

This uses the fact that 2 * 2^n = 2^(n + 1). Also, by our rules, using currying instead of taking multiple arguments the usual way is allowed. Also, I don't think the let is needed.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.