Skip to content

Commit 742f089

Browse files
committed
Power of Four
1 parent 841d023 commit 742f089

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Diff for: Power of Four.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
2+
3+
Example:
4+
Given num = 16, return true. Given num = 5, return false.
5+
6+
Follow up: Could you solve it without loops/recursion?*/
7+
8+
/**
9+
* @param {number} num
10+
* @return {boolean}
11+
*/
12+
var isPowerOfFour = function(num) {
13+
if(num <= 0)
14+
return false
15+
var power = Math.round(Math.log(num)/Math.log(4))
16+
return Math.pow(4,power) == num
17+
};

0 commit comments

Comments
 (0)