-2

How can i convert decimal to binary?

Here is my js code:

while (decimal >= 2) {
    var dec2 = decimal;

    if (decimal % 2 == 0) {
      rests.push(0);
      decimal /= 2;
    } else {
      rests.push(1);
      decimal -= 1;
      decimal /= 2;
    }
  }
  rests.push(1);
  display.textContent = rests.join("");

I think it's too much is there anyway to do that in few lines of code?

1

1 Answer 1

0

Simplest way to do that -> Number(decimal).toString(2)

Sign up to request clarification or add additional context in comments.

Comments