DEV Community

Cesar Del rio
Cesar Del rio

Posted on • Updated on

#19 - Binary Addition CodeWars Kata (7 kyu)

Instructions

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)


My solution:

function addBinary(a,b){
  return (a+b).toString(2)
}

Enter fullscreen mode Exit fullscreen mode

Explanation

First I just summed the numbers and after that I just converted it to string with a 2 parameter so it would only be 0 and 1 (binary code)


Did you like this solution? 👇🤔

My Github
My twitter
Solve this Kata

Top comments (2)

Collapse
 
mattsptc profile image
Mattsptc

Very good and simple solution

Collapse
 
cesar__dlr profile image
Cesar Del rio

Thanks for your comment bro!!