Question:
Given an integer, write a function to determine if it is a power of two.
Example:
Input: 1
Output: true
Explanation: 20 = 1
Input: 16
Output: true
Explanation: 24 = 16
Input: 218
Output: false
Solution:
/**
* @param {number} n
* @return {boolean}
*/
var isPowerOfTwo = function(n) {
let m = 1;
let j = 0
while( m < n ) {
m = Math.pow(2,j)
j++;
}
return m === n
};
Runtime: 100 ms, faster than 30.00% of JavaScript online submissions for Power of Two.