Leetcode 326 - Power of three

Note: It’s not that hard but there are some traps in this problem.

method 1:

1
2
3
4
5
6
7
8
9
10
11
var isPowerOfThree = function(n) {
let remainder = 0;
if (n === 0) return false;
if (n === 1) return true;
while (remainder === 0) {
remainder = n % 3;
n = n / 3;
if (n === 1) return true;
}
return Number.isInteger(n);
};

method 2:

1
2
3
4
5
6
7
8
var isPowerOfThree = function(n) {
for (let x = 1; x <= n; x *= 3) {
if (x === n) {
return true
}
}
return false
};