Skip to content

Commit 56a4976

Browse files
committed
Added easy/powers_of_two
1 parent c10a262 commit 56a4976

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

easy/powers_of_two.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Have the function powersOfTwo(num) take the num parameter being passed which
3+
* will be an integer and return the string true if it's a power of two. If it's
4+
* not return the string false. For example if the input is 16 then your program
5+
* should return the string true but if the input is 22 then the output should
6+
* be the string false.
7+
*
8+
* https://www.coderbyte.com/results/bhanson:Powers%20of%20Two:JavaScript
9+
*
10+
* @param {number} num
11+
* @return {string} 'true' or 'false'
12+
*/
13+
function powersOfTwo(num) {
14+
return Number.isInteger(Math.log(num) / Math.log(2)) ? 'true' : 'false';
15+
}
16+
17+
module.exports = powersOfTwo;

easy/powers_of_two.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const powersOfTwo = require('./powers_of_two');
2+
3+
describe('powersOfTwo()', () => {
4+
test("returns 'true' for power of two", () => {
5+
expect(powersOfTwo(16)).toBe('true');
6+
expect(powersOfTwo(256)).toBe('true');
7+
});
8+
9+
test("returns 'false' for not power of two", () => {
10+
expect(powersOfTwo(22)).toBe('false');
11+
expect(powersOfTwo(257)).toBe('false');
12+
});
13+
});

0 commit comments

Comments
 (0)