JSFiddle - React, Tailwind, and code Playground
by Abdul Ahmad
HTML
<div>
<input type='radio' name='conversion-type' value='int-to-loc-num'/>
<label>
integer to location numeral
</label>
</div>
<div>
<input type='radio' name='conversion-type' value='loc-num-to-int'/>
<label>
location numeral to integer
</label>
</div>
<div>
<input type='radio' name='conversion-type' value='abbreviate-loc-num'/>
<label>
abbreviate location integer
</label>
</div>
<div id='function-type'>
</div>
<div>
<input type='text' id='initial-value'/>
<button id='get-conversion'>
just do it
</button>
</div>
<div id='input-val'>
</div>
<div id='result'>
</div>
JavaScript
function getFuncType(conversionType) {
var test = new decimalLocNum();
var inputToFunctionMap = {
'int-to-loc-num': test.intToLocNum,
'loc-num-to-int': test.locNumToInt,
'abbreviate-loc-num': test.abbreviateLocNum
};
return inputToFunctionMap[conversionType];
}
$(function() {
var conversionType = '';
var value = '';
$('input[name="conversion-type"]').on('change', function() {
conversionType = $(this).val();
$('#function-type').text(conversionType);
});
$('#initial-value').on('input', function() {
value = $(this).val();
$('#input-val').text(value);
});
$('#get-conversion').on('click', function() {
var result = getFuncType(conversionType)(value);
alert(result);
$('#result').text(result);
});
});
function decimalLocNum() {
var self = this;
//set functions to instance
self.intToLocNum = intToLocNum;
self.locNumToInt = locNumToInt;
self.abbreviateLocNum = abbreviateLocNum;
//initialize maps
var locNumToIntMap = getLocNumToIntMap();
var intToLocNumMap = getIntToLocNumMap();
var locNumIntVals = Object.keys(intToLocNumMap);
//locNum === location numeral
function intToLocNum(originalInt) {
var tempInt = originalInt;
var locNum = [];
//subtract from the integer until we get to 0
//make sure we get the largest location numeral each time
//this way its the shortest possible
while(tempInt > 0) {
var nextLocNum = '';
if(locNumIntVals.indexOf('' + tempInt) > -1) {
nextLocNum = intToLocNumMap[tempInt];
} else {
nextLocNum = getLargestLocNumFromNumber(tempInt);
}
locNum.push(nextLocNum);
tempInt -= locNumToIntMap[nextLocNum];
}
return locNum.reverse();
}
function locNumToInt(locNum) {
var total = 0;
for(var i = 0; i < locNum.length; i++) {
total += locNumToIntMap[locNum[i]];
}
return total;
}
function abbreviateLocNum(locNum) {
var int = locNumToInt(locNum);
return intToLocNum(int);
}
...