The Binary Converter

MediumAcc. 92.4%
+30 XP 12

0s and 1s

Computers think in binary. To convert a decimal number to binary, you repeatedly divide by 2 and track the remainders in reverse order.

The Assignment

Your function receives a parameter named decimal.

  1. Initialize an empty string binary to store the result.
  2. If decimal is 0, print "0".
  3. Use a while loop that continues as long as decimal is greater than 0.
  4. In each step:
    • Calculate the remainder (decimal % 2).
    • Add the remainder to the front of your binary string.
    • Update decimal by dividing by 2 and rounding down (Math.floor).
  5. After the loop, print the binary string.

01EXAMPLE 1

Inputdecimal = 10
Output1010

Explanation: 10 -> 5(0) -> 2(1) -> 1(0) -> 0(1) => 1010

Constraints

  • Do not use toString(2).
  • Use a loop to perform the conversion.
MathLoopsBinary
JavaScript
Loading...
3 Hidden

Input Arguments

decimal10

Expected Output

1010

Click RUN to test your solution