Showing posts with label binary to decimal converter in javascript. Show all posts
Showing posts with label binary to decimal converter in javascript. Show all posts

Friday, October 23, 2015

Binary To Decimal Converter in JavaScript

A simple program that I wrote in JavaScript that will ask the user to enter a binary value and then convert the given binary value into decimal format.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.



Sample Program Output

Program Listing

<html>
 <head>
  <title>Binary To Decimal Converter </title>
 </head>
    <body>
<h2> Binary To Decimal Converter </h2>
    <script>
function binary_to_decimal( binaryNumber ) {
        
   
   var decimalNumber = 0;


   var placeValue = 1;

  
   while ( binaryNumber > 0 ) {

     
     var remainder = binaryNumber % 10;

      var digitValue = remainder * placeValue;

     decimalNumber = decimalNumber + digitValue;

       placeValue *= 2;

     
     binaryNumber = Math.floor( binaryNumber / 10 );
   }

   
   return decimalNumber;
 }
    var input = parseInt(prompt("Enter a Number"));
input_solve = binary_to_decimal(input);
if (!isNaN(input)) {
  document.write("<h2>" +input + "<sub>2</sub> in binary is equivalent to " 
  + input_solve + "<sub>10</sub> in decimal. </h2>" );
 }
while(isNaN(input)){
input = parseInt(prompt("Please enter a numerical value."));
input_solve =binary_to_decimal(input);
if (!isNaN(input)) {
  document.write("<h2>" +input + "<sub>2</sub> in binary is equivalent to " 
  + input_solve + "<sub>10</sub> in decimal.</h2>" );
 }
}
</script>
</body>
</html>