Does anyone know why my “Carry” variable in the code returns NAN instead of 0
var addBinary = function(a, b) {
let i = a.length - 1
let j = b.length - 1
let result = []
var carry = 0;
while (i >= 0 || j >= 0) {
if (i == 0) {
console.log(carry)
}
let sum = carry
if (i >= 0) {
sum = sum + parseInt(a[i])
}
if (j >= 0) {
sum += parseInt(b[i])
}
result.unshift(parseInt(sum) % 2)
carry = parseInt(sum) / 2
i--
j--
}
if (carry > 0) {
result.unshift(1)
}
// console.log(result)
return result.toString()
};
abbBinary('11', '1')
console.log(carry) should return 0 and not NAN. I can’t seem to find the issue, sure I am missing something