Quantcast
Channel: How to check if a number is a power of 2 - Stack Overflow
Viewing all articles
Browse latest Browse all 38

Answer by Aleksandar Biševac for How to check if a number is a power of 2

$
0
0

This one returns if the number is the power of two up to 64 value ( you can change it inside for loop condition ("6" is for 2^6 is 64);

const isPowerOfTwo = (number) => {  let result = false;  for (let i = 1; i <= 6; i++) {    if (number === Math.pow(2, i)) {      result = true;    }  }  return result;};console.log(isPowerOfTwo(16));console.log(isPowerOfTwo(10));

Viewing all articles
Browse latest Browse all 38

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>