Javascript tips: 2 to the power of x multiplied by n

6

1

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.

Avi Jain

Posted 2016-08-21T14:09:35.817

Reputation: 69

6You can try n<<x. – Leaky Nun – 2016-08-21T14:11:53.103

1FYI there isn't much point in a Stack Snippet if you aren't going to build it into some HTML – Beta Decay – 2016-08-21T14:39:03.340

Ah. That did it :) @βετѧΛєҫαγ Point noted. – Avi Jain – 2016-08-21T14:41:37.720

Answers

11

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.

Loovjo

Posted 2016-08-21T14:09:35.817

Reputation: 7 357