-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddBinary.js
60 lines (57 loc) · 1.76 KB
/
AddBinary.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"use strict"
/**
* @param {string} a
* @param {string} b
* @return {string}
*/
/* Results:
Runtime: 73 ms, faster than 75.11% of JavaScript online submissions for Add Binary.
Memory Usage: 43.5 MB, less than 49.60% of JavaScript online submissions for Add Binary.
*/
var addBinary = function (a, b) {
{
//Task: add two strings of binary numbers -> return binary sum
//Plan:
//Create variable char carry
//Create variable string result
//Create variable int x set to longer input strings length
//Create variable accumulator to hold each iteration's number
//Iterate strings from end applying single digit binary arithmetic.
//Build result with each iteration
//Return result.
}
let carry = "0";
let result = "";
let temp = "";
for (let x = a.length - 1, y = b.length - 1; x >= 0 || y >= 0 || carry === "1"; x--, y--) {
let stringA = (x >= 0) ? a[x] : "0";
let stringB = (y >= 0) ? b[y] : "0";
temp = carry + stringA + stringB;
switch (temp) {
case "000":
result = "0" + result;
carry = "0";
break;
case "100":
case "010":
case "001":
result = "1" + result;
carry = "0";
break;
case "101":
case "011":
case "110":
result = "0" + result;
carry = "1";
break;
case "111":
result = "1" + result;
carry = "1"
break;
default:
console.error("There's a bug somewhere!");
}
}
return result;
};
console.log('Correct(100) => ' + addBinary("11", "1"));