-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path067.js
More file actions
33 lines (29 loc) · 687 Bytes
/
067.js
File metadata and controls
33 lines (29 loc) · 687 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
/**
* @param {string} a
* @param {string} b
* @return {string}
*/
var addBinary = function(a, b) {
let indexA = a.length - 1;
let indexB = b.length - 1;
let result = "";
let left = 0;
while(indexA >= 0 || indexB >= 0) {
let aValue = 0;
let bValue = 0;
if (indexA >= 0) aValue = parseInt(a[indexA]);
if (indexB >= 0) bValue = parseInt(b[indexB]);
let newValue = aValue + bValue + left;
if (newValue >= 2) {
left = 1;
newValue = newValue - 2;
} else {
left = 0;
}
result = newValue + result;
if (indexA >= 0) --indexA;
if (indexB >= 0) --indexB;
}
if (left > 0) result = left + result;
return result;
};