Skip to content

Instantly share code, notes, and snippets.

@augusto-altman
Created October 16, 2015 19:19
Show Gist options
  • Save augusto-altman/8503507a30cd8b4a7b75 to your computer and use it in GitHub Desktop.
Save augusto-altman/8503507a30cd8b4a7b75 to your computer and use it in GitHub Desktop.
var romanMap = [
{
I: 'I',
V: 'V',
X: 'X'
}, {
I: 'X',
V: 'L',
X: 'C'
}, {
I: 'C',
V: 'D',
X: 'M'
}
];
function rest(num, base){
var big = Math.floor(num/base)
return {
big: big,
small: num - big*10
};
}
function subConvert(num, I, V, X) {
if(num === 1) {
return I;
} else if(num === 2) {
return I+I;
} else if(num === 3) {
return I+I+I;
} else if(num === 4) {
return I+V;
} else if(num === 5) {
return V;
} else if(num === 6) {
return V+I;
} else if(num === 7) {
return V+I+I;
} else if(num === 8) {
return V+I+I+I;
} else if(num === 9) {
return I+X;
}
}
function convert(num) {
var cont = 0;
var res = '';
for(var r = rest(num, 10); r.big > 0; r = rest(r.big, 10)) {
res = subConvert(r.small, romanMap[cont].I, romanMap[cont].V, romanMap[cont].X) + res;
cont++;
}
return subConvert(r.small, romanMap[cont].I, romanMap[cont].V, romanMap[cont].X) + res;
}
convert(16);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment