My daughter and I share a special number - 823.  We text it to eachother when the clock says 8:23 or leave a little note with it there.  We also share other instances of the number that we come accross. I sent these photos to her recently:
The address is 823
823 in Binary 1100110111
It's a nice way to let each other know that we are thinking about one another.  Lately, I was wondering what 823 looked like in other bases besides decimal(10) and binary(2). So I wrote a recursive javascript function called convert to do the job.  The user can enter a decimal number and new base to see the equivelant value with the new radix.
<!DOCTYPE HTML>
<html>
<script>
   function doit(){
      var decimal = parseInt(document.getElementById('decimal').value);
      var newBase = parseInt(document.getElementById('newBase').value);
      var output = document.getElementById('output');
      if(newBase > 1 && newBase <10)      
         output.innerHTML = convert(decimal, newBase);
   }
   
   function convert(quotient, base){
      if(quotient > 0){
        var remainder = quotient % base;
        return convert(parseInt(quotient / base), base) + remainder.toString();
      }
      return '';
   }
</script>
<body>
    Decimal: <input type="text" id="decimal">
    New Base:   <input type="text" id="newBase">
    <button type="button" label="doit" onclick="doit();">do it</button>
    <div id="output"></div>
</body>
</html>
And here is the result.  You can enter a decimal number and a base between 2 and 9 inclusively. 
Decimal: New Base:

Published by thorshov

Scott Thorshov 1973

Leave a comment

Your email address will not be published. Required fields are marked *