Base 10 To Base B

by black strings

HTML

<div id="main">

</div>

CSS

.display{margin:5px; border:thin solid #ccc; padding:5px; width:100px; height:30px;}
.inputs{margin:0 10 0 0}

JavaScript

var input1 = $('<input>').addClass("inputs").val('value');
var input2 = $('<input>').addClass("inputs").val('base');
var display = $('<div>').addClass("display");

//an string array to contain the remainder like [3,5,F,1]
//you can't hold 10, or 11 like this [3,10,3,5,11]
//you have to convert it to hex character ABCE...
//Where 10 is A, and 11 is B, and so on throughout the alphabet
var arrNum = [];

var btn = $('<button>').text("Convert").click(function(){
		baseFinder(input1.val(), input2.val(), arrNum);
    arrNum.reverse();
			var arrNumStr = "";
			for(var i=0; i<arrNum.length; i++){
				arrNumStr += arrNum[i];
			}
      display.text(arrNumStr);
      arrNum = [];
	});
$('#main').append(input1).append(input2).append(display).append(btn);

//The recursive logic method
//v = value, b = base which are pretty much the user input
//arrNum is the array container to hold the remainders
//therefore, arrNum should be created outside the method and passed in
var baseFinder = function(v,b, arrNum){

	//determines if the recusrive should force stop
  //in this case, when our base is set higher than 36
  var errorFlag = false;
  
  //we need two variables: divide and mod
  var quot = v / b;	//helps us know if we reached zero
  var rem = v % b;	//helps use know if we have remainders

	//a quotient > 0 means there's a chance for another recursive
  //quot of zero means stop the recursive
  if(quot > 0){
  
  	//hexa conversion when you get a remainder 10 or higher
  	if(rem > 9){
    	//the map of hex characters we can get using conversion
      var hexArray = ['A','B','C','D','E','F','G','H','I','J','K',
      						'L','M','N','O','P','Q','R','S','T','U','V','W',
                  'X','Y','Z'];
                  
      //start index at zero by subtracting 10 off the bat
      //ex: 
      //if remainer (rem) is 10
      //index = (rem - 10)
      //index would be 0
      //if we do hexMap[index] we would get 'A'
      var index =  rem - 10;
      
      //allow up to...