Add another version of power() function

This commit is contained in:
helloShen 2022-02-07 20:54:14 -05:00
parent 3348f54715
commit 997cb06a7f
1 changed files with 17 additions and 0 deletions

View File

@ -20,6 +20,23 @@ const power = function(a, b) {
return Math.pow(a, b);
};
/*
* If you want to write a handmade power() function by yourself,
* Here's a simple example.
* Only work with integer as input.
*/
const intPower = function(base, exponent) {
if (exponent === 0) return 1;
if (base === 0) return (exponent > 0)? 0 : Infinity;
let result = 1;
if (exponent > 0) {
while (exponent-- > 0) result *= base;
} else {
while (exponent++ < 0) result /= base;
}
return result;
};
const factorial = function(n) {
if (n < 0) return undefined;
if (n === 0) return 1;