Skip to content

Instantly share code, notes, and snippets.

Created December 3, 2016 08:07
Show Gist options
  • Save anonymous/fcd609ac2c9d3b6bbcca508ae07eabaa to your computer and use it in GitHub Desktop.
Save anonymous/fcd609ac2c9d3b6bbcca508ae07eabaa to your computer and use it in GitHub Desktop.
is Power of Two created by wwebby1 - https://repl.it/E87A/2
// # Write a method that takes in a number and returns true if it is a
// # power of 2. Otherwise, return false.
// #
// # You may want to use the `%` modulo operation. `5 % 2` returns the
// # remainder when dividing 5 by 2; therefore, `5 % 2 == 1`. In the case
// # of `6 % 2`, since 2 evenly divides 6 with no remainder, `6 % 2 == 0`.
// #
function isPowerOf2(num){
var base = Math.log2(num);
if(Number.isInteger(base))
return true;
else
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment