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

605
Views
JavaScript program to convert binary to decimal

I am trying to make a program which is able to do both decimal to binary and binary to decimal conversions.

I am having trouble with the binary to decimal portion of the code. Forgive me as I know the coding is quite incomplete but I can't figure out where I am going wrong.

Currently I am getting partially correct output in the calculation field (ex. "there is a 1 in the value of (2^0)" and "there is a 2 in the value of (2^1)").

However, when I type 11 as decimal the calculation field is repeating the code twice (ex. "there is a 1 in the value of (2^0)","there is a 2 in the value of (2^1)","there is a 1 in the value of (2^0)", "there is a 2 in the value of (2^1)").

Obviously it should only give those values once per number.

Also the output field for the actual binary number is incorrect as well, and some of the variables aren't utilized/not needed, but I have been trying to fix the problem of repeating values first before I worked on that.

Any help would be much appreciated!!

function convertByArray(bval) {
   
    var rB = new Array();
    var outstr = "";
    var p, t, a, o;
    
    o = 0;
    for(var i=0; i<bval.length; i++) {
        var b = bval.charCodeAt(i);  
        t = 2;
        p = i;
        a = t ** p;
        
        if(a === t ** p) {
           outstr += a;
        }
        var bV = b;
        $("txtCalc").value += "There is a " + a + " in the value " + "(" + t + "^" + p + ")" + "\n";
     
        o += 1;  
        b = bV;
        $("txtOut").value = outstr;
    }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can simply your code if you access the most-significant bit of the bit-string by taking the length (minus one) and subtracting it from the current position. You can access string characters like an array.

var $txtCalc = $(".txtCalc");
var $txtOut = $(".txtOut");

binaryToDecimal("10010101"); // 149

function binaryToDecimal(bval) {
  var base = 2, result = 0;
  for (var pos = 0; pos < bval.length; pos++) {
    var bit = +bval[(bval.length - 1) - pos];
    if (bit === 1) {
      result += base ** pos;
    }
    var message = "There is a " + bit + " in the position (" + base + "^" + pos + ")";
    $txtCalc.val($txtCalc.val() + message + "\n");
  }
  $txtOut.val(result);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="txtOut" />
<br />
<textarea class="txtCalc" rows="10" cols="60"></textarea>

Alternatively, you can simply your program to the following. In JavaScript, you can parse any number in any base and format to another base.

var $txtOut = $(".txtOut");

binaryToDecimal("10010101"); // 149

function convertFromBaseToBase(number, fromBase, toBase) {
  return parseInt(number, fromBase).toString(toBase);
}

function binaryToDecimal(bval) {
  $txtOut.val(convertFromBaseToBase(bval, 2, 10));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="txtOut" />

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!