Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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