Skip to content

Instantly share code, notes, and snippets.

@layflags
Created January 18, 2019 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save layflags/480a22cedea6ad46936f94659e017777 to your computer and use it in GitHub Desktop.
Save layflags/480a22cedea6ad46936f94659e017777 to your computer and use it in GitHub Desktop.
Implementing JS' Math.pow functional
// 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