All files / leetCode 0067.ts

100% Statements 9/9
100% Branches 7/7
100% Functions 1/1
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16  3x   3x 3x       107x 107x 107x     3x    
export default function addBinary(a: string, b: string): string {
  let solution = ''
 
  for (
    let i = a.length - 1, j = b.length - 1, carry = 0;
    i >= 0 || j >= 0 || carry;
    i--, j--, carry = Math.floor(carry / 2)
  ) {
    carry += i >= 0 ? Number.parseInt(a[i], 2) : 0
    carry += j >= 0 ? Number.parseInt(b[j], 2) : 0
    solution = (carry % 2).toString(2) + solution
  }
 
  return solution
}