Skip to content

Commit 205eb5d

Browse files
committed
Counting Bits
1 parent 4ed4d3c commit 205eb5d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

counting-bits/hyejjun.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number} n
3+
* @return {number[]}
4+
*/
5+
var countBits = function (n) {
6+
7+
let result = [];
8+
9+
for (let i = 0; i <= n; i++) {
10+
11+
let binary = i.toString(2);
12+
let sum = 0;
13+
14+
for (let char of binary) {
15+
sum += Number(char);
16+
}
17+
result.push(sum);
18+
}
19+
20+
return result;
21+
22+
};
23+
24+
25+
console.log(countBits(2));
26+
console.log(countBits(5));

0 commit comments

Comments
 (0)