Skip to content

Instantly share code, notes, and snippets.

@radovansurlak
Created April 3, 2017 21:20
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 radovansurlak/a257778f7e131ff1325031144f73dd77 to your computer and use it in GitHub Desktop.
Save radovansurlak/a257778f7e131ff1325031144f73dd77 to your computer and use it in GitHub Desktop.
JS Recursive Function + Ternary Operator - Power
function power (num, pow) {
return (pow === 0) ? 1 : num * power(num, pow-1);
}
@radovansurlak
Copy link
Author

Number.prototype.power = function (pow) {
  return (pow === 0) ? 1 : this * power(this, pow-1);
}

Here's a prototype extension, but I have read that extending prototypes might break things up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment