Skip to content

Instantly share code, notes, and snippets.

@vmlinz
Created September 13, 2015 10:28
Show Gist options
  • Save vmlinz/c053dab7a7201dcc0531 to your computer and use it in GitHub Desktop.
Save vmlinz/c053dab7a7201dcc0531 to your computer and use it in GitHub Desktop.
Convert numbers to roman notation
function convert(num) {
var romanSigns = ['I', 'V', 'X', 'L', 'C', 'D', 'M'];
var numString = num.toString();
var numToRoman = function(n, sl, sm, sh) {
var syms = [];
syms.push('', sl, sl+sl, sl+sl+sl, sl+sm, sm, sm+sl, sm+sl+sl, sm+sl+sl+sl, sl + sh);
return syms[n];
};
return numString.split('').reverse().map(function (v, i) {
return numToRoman(v, romanSigns[2 * i], romanSigns[2 * i + 1], romanSigns[2 * i + 2]);
}).reverse().join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment