DEV Community

Cesar Del rio
Cesar Del rio

Posted on • Edited on

8 3

#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!!

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay