Binary Sums

Published by Mubashir Hassan in

Adding fractional binary numbers is similar to adding decimals. The places to the right of the decimal point (or binary point) are halves, quarters, eighths instead of tenths, hundredths, thousandths, etc.

Improvise a function that takes two fractional binary numbers and produces their base-10 sum. The sum can be a whole number, a fraction, or a mixed number. The answer should be returned as a string with fractions reduced to lowest terms.

Examples

binarySum(["1.1", "1.1"]) ➞ "3"
// 1.5 + 1.5

binarySum(["11.1", "0.001"]) ➞ "3 5/8"
// 3.5 + 0.125

binarySum(["1101.0", "0.0100"]) ➞ "13 1/4"
// 13 + 1/4

binarySum(["0.11", "0.00001"]) ➞ "25/32"
// 3/4 + 1/32

binarySum(["0.0", "101.00"]) ➞ "5"

Notes

N/A

Watch a quick demo on how Edabit works.