-
Notifications
You must be signed in to change notification settings - Fork 0
/
67. Add Binary.js
46 lines (34 loc) · 1.01 KB
/
67. Add Binary.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
import * as Utils from './Utils.js';
/**
* Given two binary strings a and b, return their sum as a binary string.
*/
class Test {
method(bin1, bin2) {
bin1 = bin1.split('');
bin2 = bin2.split('');
let result = '';
let carryOver = 0;
for(let i = 0; i < Math.max(bin1.length, bin2.length); i++) {
let dig1 = bin1[bin1.length - 1 - i] ?? 0;
let dig2 = bin2[bin2.length - 1 - i] ?? 0;
const a = parseInt(dig1) + parseInt(dig2) + carryOver;
carryOver = Math.floor(a / 2);
result = `${a % 2}${result}`;
}
if (carryOver) {
result = `${carryOver}${result}`;
}
return result;
}
}
const testExecutor = new Test();
const testCase = [
{par1: '1100', par2: '1', result: '1101'},
{par1: '1010', par2: '1011', result: '10101'},
];
testCase.forEach(testCase => {
console.log('Executing test', testCase);
const result = testExecutor.method(testCase.par1, testCase.par2);
console.log('Result:', result);
console.assert(result === testCase.result);
})