Convert number to binary

I saw a solution of this challenge from codewar

The challenge instruction is as below:

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.

Here is the solution I saw on github:

function addBinary( a, b ){ var sum = a + b, binary = ''; while ( sum > 0 ) { binary = ( sum % 2 ) + binary; sum = Math.floor( sum / 2 ); } return binary; }
console.log( addBinary( 1,2 ) ); //Should be 11
console.log( addBinary( 51,12 ) ); //Should be 111111
console.log( addBinary( 100, 0 ) ); //Should be 1100100

My question is that since binary always start counting from right to left,
Why we don’t need to add anything to this code to change the binary order from right to left???

Will be very appreciate if there is anyone can explain to me.

1 Like

Because of this line. Each new digit is appended to the beginning of the string.

Note that JavaScript numbers natively support conversion to binary representations via the toString method:

(255).toString(); // "255" (default is radix 10)
(255).toString(2); // "11111111" (radix 2, i.e. binary)
(255).toString(16); // "ff" (radix 16, i.e. hexadecimal)
8 Likes

I didn’t know this until you told me! Thank you vary much!!

1 Like

So none of them return correct value when number is negative
for example (-1).toString() or (-1).toString(2) both returns “-1”

Well, if you try with -1… what did you expect?
Instead converting -2 to binary give correctly "-10"

1 Like

Why do you think that’s wrong?

Using this type of function

const convertNumberToBinary = number => {
    return (number >>> 0).toString(2);
}

can solve much of your converting problems.

let result = convertNumberToBinary (15)
// result = "1111"
let result = convertNumberToBinary (123)
// result = 1111011

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

If you want to compare your solution to others, use the Get a hint button on the challenge and there are alternative solutions you can compare yours to. Also, you can probably search older posts using the forum search feature or google the challenge name and find more there.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

Yeah, I definitely understand.My first reply in the forum sorry about that.

I have a question about this solution. The part that reads:

binary = ( sum % 2 ) + binary;
sum = Math.floor( sum / 2 ); }
return binary;
}

how does binary get updated to return the correct value?

Are you talking about this line?

Yes. If sum = Math.floor( sum / 2 ); dosent update binary, so why is it returning binary

This line updates binary based upon sum

This line decreases sum

This line returns the updated binary

Hey.
I get that part, what I’m not getting is how binary is updated. If the compiler reads top to bottom, how is binary getting updated?

it’s inside a loop so that line is executed once for each time the loop runs

2 Likes