Skip to content

Commit ffcf7c2

Browse files
authored
Update 50-powx-n.js
1 parent 2ee578c commit ffcf7c2

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

50-powx-n.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ const myPow = function(x, n) {
1717

1818
// another
1919

20+
/**
21+
* @param {number} x
22+
* @param {number} n
23+
* @return {number}
24+
*/
25+
const myPow = function(x, n) {
26+
if(n === 0) return 1
27+
if(n === 1) return x
28+
if(n < 0) {
29+
x = 1 / x
30+
n = -n
31+
}
32+
return n % 2 === 1 ? myPow(x, ~~(n / 2)) ** 2 * x : myPow(x, ~~(n / 2)) ** 2
33+
};
34+
35+
// another
36+
2037
/**
2138
* @param {number} x
2239
* @param {number} n

0 commit comments

Comments
 (0)