-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay14.js
More file actions
37 lines (25 loc) · 1007 Bytes
/
Day14.js
File metadata and controls
37 lines (25 loc) · 1007 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
32
33
34
35
36
37
// 14: Write a function called isPowerOfTwo that takes an integer num as input and returns true if num is a power of two, and false otherwise.
// Example usage:
// 2*2 =4
// 2*2*2 = 8
const isPowerOfTwo = (num) => {
for (let i = 1; i <= num; i++) {
if (2 ** i === num) {
return true;
}
}
return false;
};
console.log(isPowerOfTwo(8)); // Output: true
console.log(isPowerOfTwo(7)); // Output: false
// using bitwise operators
// const isPowerOfTwo = (num) => {
// return num > 0 && (num & (num - 1)) === 0;
// };
// console.log(isPowerOfTwo(8)); // Output: true
// console.log(isPowerOfTwo(7)); // Output: false
// Notes:
// - The input num will be a positive integer.
// - Zero (0) and negative integers are not considered powers of two.
// - The function should return true if the given number is a power of 2, and false otherwise.
// We can solve it using bitwise operators too, but it's your chance to do it and let me know in the comment section.