-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisPowerOfTwo.js
More file actions
31 lines (26 loc) · 828 Bytes
/
isPowerOfTwo.js
File metadata and controls
31 lines (26 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
Power of Two
isPowerOfTwo(4) => true
isPowerOfTwo(5) => false
*/
const isPowerOfTwo = (num) => {
let ans = 1, INT_MAX = 2147483647;;
for(let i=0; i<=30; i++)//int range is 2^31(0-30)
{
//checking ans is equal to the given num.
if(num == ans)
return true;
/*
If ans hasn't reached the maximum integer value (INT_MAX), it multiplies ans by 2 in each iteration, effectively checking the next power of two.
*/
if(ans < INT_MAX)
ans = ans * 2;//previous ans * 2
}
//If no match is found within the loop, the function returns false
return false;
}
console.log(isPowerOfTwo(1));//true
console.log(isPowerOfTwo(2));//true
console.log(isPowerOfTwo(5));//false
console.log(isPowerOfTwo(8));//true
console.log(isPowerOfTwo(11));//false