Created
September 13, 2015 10:28
-
-
Save vmlinz/c053dab7a7201dcc0531 to your computer and use it in GitHub Desktop.
Convert numbers to roman notation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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