Skip to content

Instantly share code, notes, and snippets.

@westc
Last active March 20, 2018 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westc/f65006c59fbc8d41b29c11d6fc4dcdf9 to your computer and use it in GitHub Desktop.
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/
/**
* @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