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...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.