Created
May 23, 2014 13:53
-
-
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.
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
<!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