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

97
Views
Decimal to Binary Calculator

New to coding, I just thought of an idea of converting decimal numbers to binary so I made this program.

However, while I feel the logic is right, I'm not sure why its not returning a full array of zero's and 1's corresponding to the decimal number that was input.

After I get the array, I plan on doing remainder.join('') to return as one string.

If anyone could share their thoughts that would be great.

Currently it only consoles an array like ['1'] or ['0'].

const getBinary = (num) => {
  let remainder = [];
  let quotient = Math.floor(num / 2);
  while (num != 0) {
    if ((quotient * 2) < num) {
      remainder.push('1');
    } else if ((quotient * 2) === num) {
      remainder.push('0');
    }
    num = Math.floor(num / 2);
    return remainder;
  }
};
console.log(getBinary(6));

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

0

You put the return statement in the while loop so it only loops once. You should place the return statement before the last }. Also, you should update the quotient variable in each loop.

const getBinary = (num) => {

  let remainder = [];
  let quotient = Math.floor(num / 2);

  while (num != 0) {
    if ((quotient * 2) < num) {
      remainder.push('1');
    } else if ((quotient * 2) === num) {
      remainder.push('0');
    }

    num = Math.floor(num / 2);
    quotient = Math.floor(num / 2); // added this line
    // return remainder; move this line
  }
  // to there
  return remainder;

};

console.log(getBinary(6));

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!