-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryNumConv.js
More file actions
33 lines (27 loc) · 779 Bytes
/
Copy pathbinaryNumConv.js
File metadata and controls
33 lines (27 loc) · 779 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
//Implement a function that adds two numbers together and returns their sum in binary.
//The conversion can be done before, or after the addition.
//The binary number returned should be a string.
/*
function addBinary(a,b) {
let sumOfNum = a + b
let binaryNumToConv = sumOfNum%2
let divisor = function(currentDivisableNum){
sumOfNum = currentDivisableNum/2
binaryNumToConv = currentDivisableNum%2
return sumOfNum && binaryNumToConv
}
let binaryNumArray = []
while(sumOfNum >= 1){
if(binaryNumToConv.toFixed() >= 1){
binaryNumArray.push(1)
}else{
binaryNumArray.push(0)
}
divisor(sumOfNum)
}
return binaryNumArray.reverse().concat().join('')
}
*/
function addBinary(a,b) {
return (a + b).toString(2)
}