Skip to content

Instantly share code, notes, and snippets.

@kumarldh
Created May 23, 2014 13:53
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 kumarldh/669ec77a5263be54b06a to your computer and use it in GitHub Desktop.
Save kumarldh/669ec77a5263be54b06a to your computer and use it in GitHub Desktop.
Check if a given number is power of 2. This can be modified any number less than 10.
<!DOCTYPE html>
<html>
<head>
<title>Check if a given a number is power of 2</title>
<meta charset="UTF-8">
</head>
<body>
Check if a given number is power of 2. This can be modified any number less than 10.
<script>
function isPowerOf2(input){
if(input === undefined || input < 2){//only for signed positive numbers
return false;
}
var number = 2;//will work up to 9
for(var i = 0; i < input; i++) {
if(input % number > 0){
return false;
}else{
input = input / number;
}
}
return true;
}
console.log(isPowerOf2(1000));
console.log(isPowerOf2(1200));
console.log(isPowerOf2(1230));
console.log(isPowerOf2(1234));
console.log(isPowerOf2(4));
console.log(isPowerOf2(6));
console.log(isPowerOf2(64));
console.log(isPowerOf2(81));
console.log(isPowerOf2(80));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment