Daily Coding Challenge: Binary Addition – written in JavaScript and Python

Posted by

2 min read ~ Hello readers! It’s really important for me to continue my practice and learning. Lately, I have been completing a coding challenge a day with codewars. This helps me keep my skills sharp and see how other devs all over the planet would solve these problems. Being able to see other solutions expands my mind for solving future problems in a clever and efficient way. I’ve decided that I am going to post my solutions here written in JavaJavaScript and Python for all of my readers to learn! Please let me know if you have any questions. This problem only let me solve it in JS and Python.

Binary Addition

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.


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

Ex Input: addBinary(1, 2)
Ex Output: '11'


def add_binary(a,b):
    return "{0:b}".format(a + b)

Ex Input: addBinary(1, 2)
Ex Output: '11'

Disclaimer: *Now, as much as I am tempted to use ES8 & ES9 functions to reduce the number of lines in JavaScript, I am always a fan of readable code that others can understand. Therefore, with the JavaScript examples, I will try to write it without fancy functions as much as possible so it can actually be read and absorbed by my readers. All of my solutions are commented to explain my thinking. Of course, they could always be better. Please feel free to share how you would solve it!*