DEV Community

Miss Pooja Anilkumar Patel
Miss Pooja Anilkumar Patel

Posted on • Updated on

326.Leetcode Solution in JavaScript

/**
 * @param {number} n
 * @return {boolean}
 */
var isPowerOfThree = function(n) {
    if (n === 0) return false;
    if (n === 1) return true;
    return n % 3 === 0 && isPowerOfThree(Math.floor(n / 3));
};

// iterative Ways
var isPowerOfThree = function(n) {
    if (n > 1) {
        while (n % 3 === 0) {
            n = Math.floor(n / 3);
        }
    }
    return n === 1;
};
Enter fullscreen mode Exit fullscreen mode

leetcode

challenge

Here is the link for the problem:
https://leetcode.com/problems/power-of-three/

Top comments (0)