Question:

Given an integer, write a function to determine if it is a power of three.

Example:

  1. Input: 27
  2. Output: true
Input: 0
Output: false

Solution:

/**
 * @param {number} n
 * @return {boolean}
 */
var isPowerOfThree = function(n) {
  if( n === 0) return false;
  let m = 0;
  let j = 0;
  while (m < n) {
    m = Math.pow(3, j);
    j++;
  }
  return m === n;
};

Runtime: 360 ms, faster than 36.36% of JavaScript online submissions for Power of Three.