RosettaCodeData/Task/Exponentiation-operator/JavaScript/exponentiation-operator.js

13 lines
255 B
JavaScript
Raw Permalink Normal View History

2013-04-10 16:57:12 -07:00
function pow(base, exp) {
if (exp != Math.floor(exp))
throw "exponent must be an integer";
if (exp < 0)
return 1 / pow(base, -exp);
var ans = 1;
while (exp > 0) {
ans *= base;
exp--;
}
return ans;
}