cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <!-- Creating a web implementation to replicate a in class task to convert a binary input to decimal
WIP: write up can be found at my blog: http://www.javascriptunscripted.com/?p=158
-->
<html>
  <head>
  <title>Binary to Decimal convertor</title></head>
<body>
  <h1>Binary to Decimal convertor: [WIP]</h1>
  Binary form <input id="userInput" type="text" placeholder="enter binary here"></input>
  <button onclick="convertToBinary()">Convert</button>
  
  Decimal <input id="output"></input>

<div id="oneliner">
  <p>Creating a web implementation to replicate in class task of converting a binary input to decimal
    WIP: write up can be found at my blog: <a href="http://www.javascriptunscripted.com/?p=158" target="_blank">www.jsunscripted.com</a></p>
</div>
  </body>
</html>
            
          
!
            
              html {
  font: 15px Verdana, sans-serif;
}

#oneliner{
  font-size: 0.6em;
  position: absolute;
  bottom: 1%;
}
            
          
!
            
              var convertToBinary = function(){
var binInput = document.getElementById("userInput").value;  
  document.getElementById("output").value = checkInput(binInput);

  console.log(binInput);
}


/* Method to compare whether input conforms to Binary values*/
var checkInput = function(input){
  if(/[^10]/g.test(input)){
    console.log("Value entered is not in binary form");
    return "binary inputs only";
     } else {
      console.log("You've entered a binary number");
       return convertToDecimal(input);
    }
}

var convertToDecimal = function(binVal){
  var result = 0;
  var n = binVal.length - 1;
  for (i = 0; i < binVal.length; i++){
    var digit = binVal.charAt(i);
    result = result + (digit * Math.pow(2, n));
    n--;
  }
  return result;
}

            
          
!
999px
Loading ..................

Console