r/learnjavascript • • May 13 '21

Exponent Power

Post image
323 Upvotes

18 comments sorted by

46

u/grantrules May 13 '21

Extra longhand!

const power = ((num,exp) => Array.from({length: exp}, () => num).reduce((a,b) => a*b))(4,3)

/obviously not serious

22

u/spinelust May 13 '21

Extra extra longhand would be fetching the result from external API xD.

17

u/zerik100 May 13 '21

And the API queries the equation from a Database that has stored the harcoded solution.

5

u/[deleted] May 13 '21

Compile time optimization

13

u/awekening_bro May 13 '21 edited May 13 '21

When recursion is bae

const pow = (base, i) => {
    If(i === 0) return 1;
    return base * pow(base, i - 1);
}

4

u/Successful-Shape272 May 13 '21

Nice I learned something new ty

4

u/danielsan1701 May 13 '21
// Barking up the wrong tree
const notPower = 4^3; // 7

4

u/Its_Stev03 May 13 '21

Isnt the carat a bitwise operator? I think it is exclusive or (XOR)

1

u/[deleted] May 14 '21

Thats the joke

1

u/fukitol- May 14 '21

Yep that's right

3

u/Chef619 May 13 '21

Has anyone ever used this? <- not a snarky comment.

1

u/jack_waugh May 13 '21

It's needed for compound interest.

1

u/samanime May 13 '21

I had to use it as part of automatically calculating an even-number axis for a graph (specifically, used to determine the magnitude of the number).

1

u/PresidentZeus May 14 '21

if you're making a calculator

0

u/Sea-Nectarine5748 May 13 '21

keep em coming