Last active
March 20, 2018 12:49
-
-
Save westc/f65006c59fbc8d41b29c11d6fc4dcdf9 to your computer and use it in GitHub Desktop.
Determines if one number (x) is an even power of a given base. Check out http://cwestblog.com/2018/03/20/javascript-snippet-ispowerof-x-base/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @preserve isPowerOf() by Chris West | |
* http://cwestblog.com/2018/03/20/javascript-snippet-ispowerof-x-base/ | |
* | |
* Determines if one number (x) is an even power of a given base. | |
* @name isPowerOf | |
* @function | |
* @param {number} x | |
* Number that could be the result of multiplying or dividing base | |
* by itself a certain number of times. | |
* @param {number} base | |
* Number that will be checked to see if it is the base that when | |
* multiplied or divided by itself a certain amount of times will | |
* result in x. | |
* @returns {boolean} | |
* Returns `true` if when base is multiplied or divided by itself | |
* a certain number of times it will result in x. | |
*/ | |
function isPowerOf(x, base) { | |
x = Math.log(x) / Math.log(base); | |
return x === ~~x; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment