Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
How to convert real number between 0 to 1 into binary in Javascript?

I am trying to convert a real number (a) which is between zero and one to an array of bits.

I have tried this:

let a = 0.625
let b = []
while (a != 0) {
    if ((a * 2) >= 1) {
        b.push(1)
        a = a - 1          
    }    
    else {
        b.push(0)  
    }
}
console.log(b)

But I get an error that says "something went wrong while displaying this webpage".

Can you help me? Thanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Because you just test if a * 2 is larger 1, but never actually multiply it by two you enter a infinite loop. You could do:

let a = 0.625
let b = []
while (a != 0) {
    a *= 2

    if (a >= 1) {
        b.push(1)
        a = a - 1
        
    }
    
    else {
        b.push(0)

    }
}
console.log(b)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!