Skip to content

Instantly share code, notes, and snippets.

@omarduarte
Last active February 11, 2023 19:18
Show Gist options
  • Save omarduarte/a05878748e24068251cb to your computer and use it in GitHub Desktop.
Save omarduarte/a05878748e24068251cb to your computer and use it in GitHub Desktop.
Description:
In this kata you have to implement a base converter, which converts between
arbitrary bases / alphabets. Here are some pre-defined alphabets:
var Alphabet = {
BINARY: '01',
OCTAL: '01234567',
DECIMAL: '0123456789',
HEXA_DECIMAL: '0123456789abcdef',
ALPHA_LOWER: 'abcdefghijklmnopqrstuvwxyz',
ALPHA_UPPER: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
ALPHA: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
ALPHA_NUMERIC: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
};
The function convert() should take an input (string), the source alphabet (string)
and the target alphabet (string). You can assume that the input value always
consists of characters from the source alphabet. You don't need to validate it.
Examples:
// convert between numeral systems
convert("15", Alphabet.DECIMAL, Alphabet.BINARY); // should return "1111"
convert("15", Alphabet.DECIMAL, Alphabet.OCTAL); // should return "17"
convert("1010", Alphabet.BINARY, Alphabet.DECIMAL); // should return "10"
convert("1010", Alphabet.BINARY, Alphabet.HEXA_DECIMAL); // should return "a"
// other bases
convert("0", Alphabet.DECIMAL, Alphabet.ALPHA); // should return "a"
convert("27", Alphabet.DECIMAL, Alphabet.ALPHA_LOWER); // should return "bb"
convert("hello", Alphabet.ALPHA_LOWER, Alphabet.HEXA_DECIMAL); // should return "320048"
convert("SAME", Alphabet.ALPHA_UPPER, Alphabet.ALPHA_UPPER); // should return "SAME"
Additional Notes:
The maximum input value can always be encoded in a number without loss of precision
The function must work for any arbitrary alphabets, not only the pre-defined ones
You don't have to consider negative numbers
/*
* I was practicing Recursion, Object methods, and functional programming.
* Not an ideal solution is found, but meh...
*/
function convert(input, source, target) {
// I convert everything to Base10 and then to all other
function Converter(valueStr, source){
this.src = {value: valueStr,
alphabet: source,
base: source.length}
this.decimal = this.toBase10();
}
Converter.prototype.toBase10 = function(){
var srcValueArray = this.src.value.split("");
var srcAlphabet = this.src.alphabet;
var base = this.src.base;
function alphabetToDecimal(char){
return srcAlphabet.indexOf(char);
}
function reduceDecimal(array,result){
var result = result || 0;
result += Math.pow(base,array.length-1) * array.shift();
if(array.length === 0)
return result;
else
return reduceDecimal(array,result);
}
return reduceDecimal(srcValueArray.map(alphabetToDecimal));
}
Converter.prototype.toBase = function(base,num,result){
var result = result || [];
var num = num || this.decimal;
result.unshift(num % base);
if (num < base)
return result;
else
return this.toBase(base,parseInt(num / base),result);
}
Converter.prototype.toAlphabet = function(alphabet){
var alphabetBaseIntArray = this.toBase(alphabet.length);
function charMapper(el){
return alphabet.charAt(el);
}
return alphabetBaseIntArray.map(charMapper).join("");
}
// Start
//============================
//============================
var number = new Converter(input, source);
return number.toAlphabet(target);
}
@omarduarte
Copy link
Author

I started reading 'Maintanable Javascript' after finishing this kata. I realize now all these code conventions I'm breaking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment