Skip to content

#180 Binary Addition

Adam Kubiś edited this page Oct 28, 2026 · 1 revision

180 Binary Addition

Create a function that takes two numbers and returns their sum as a binary string.

Current source and tests | Catalog | Testing and tooling

Run this solution

From the cloned repository root, after installing dependencies:

node "Edabit/180 Binary Addition.js"

Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.

Current implementation and tests

This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.

//Create a function that takes two numbers and returns their sum as a binary string.
function addBinary(a, b) {
    return (a + b).toString(2);
}
import { assertEquals } from '../testHelper.js';
assertEquals(addBinary(1, 1), '10')
assertEquals(addBinary(1, 2), '11')
assertEquals(addBinary(4, 5), '1001')
assertEquals(addBinary(8, 20), '11100')
assertEquals(addBinary(100, 20), '1111000')
assertEquals(addBinary(40, 50), '1011010')
assertEquals(addBinary(65, 77), '10001110')
assertEquals(addBinary(40, 50), '1011010')
assertEquals(addBinary(1, 0), '1')

Clone this wiki locally