Binary Addition

Lee·2022년 7월 5일

Algorithm

목록 보기
38/92
post-thumbnail

❓ Build a pile of Cubes

Q.Implement a function that adds two numbers together and returns their sum in binary. The conversion can be done before, or after the addition.

The binary number returned should be a string.

Examples:(Input1, Input2 --> Output (explanation)))

1, 1 --> "10" (1 + 1 = 2 in decimal or 10 in binary)
5, 9 --> "1110" (5 + 9 = 14 in decimal or 1110 in binary)

✔ Solution

//#my solution
function addBinary(a, b) {
  let result = a + b;
  return result.toString(2);
}


//#other solution
const addBinary = (a, b) => (a + b).toString(2);
profile
Lee

0개의 댓글