Skip to content

Commit ca8a47c

Browse files
authored
Create 3280-convert-date-to-binary.js
1 parent 616f5ff commit ca8a47c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

3280-convert-date-to-binary.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {string} date
3+
* @return {string}
4+
*/
5+
var convertDateToBinary = function(date) {
6+
let temp = [], res =[]
7+
for(let i of date){
8+
if(i=='-'){
9+
temp = dec_to_bin(temp.join(''))
10+
res.push(...temp)
11+
temp = []
12+
res.push('-')
13+
}
14+
else temp.push(i)
15+
}
16+
temp = dec_to_bin(temp.join(''))
17+
res.push(...temp)
18+
return res.join('')
19+
};
20+
21+
function dec_to_bin( a){
22+
let b = +(a)
23+
let res = []
24+
while(b>0){
25+
if(b&1)res.push('1')
26+
else res.push('0')
27+
b>>=1
28+
}
29+
res.reverse()
30+
return res
31+
}

0 commit comments

Comments
 (0)