Created
January 18, 2019 11:03
-
-
Save layflags/480a22cedea6ad46936f94659e017777 to your computer and use it in GitHub Desktop.
Implementing JS' Math.pow functional
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// recursive | |
const pow = (base, expo) => | |
expo === 1 | |
? base | |
: base * pow(base, expo - 1); | |
// tail recursive | |
const pow = (base, expo, _result = base) => | |
expo === 1 | |
? _result | |
: pow(base, expo - 1, base * _result); | |
// reduce | |
const pow = (base, expo) => | |
Array(expo).fill(base).reduce((r, v) => r * v); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment