Created
January 9, 2017 07:51
-
-
Save terakilobyte/ce4b37c8b3efe9bb23ca745eebdfda14 to your computer and use it in GitHub Desktop.
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 convertToRoman (num) { | |
let romans = [['I', 'V', 'X'], ['X', 'L', 'C'], ['C', 'D', 'M'], ['M']] | |
function getOutput (number, one, five, ten) { | |
var romanMap = { | |
0: () => ‘’, | |
1: one => `${one}`, | |
2: one => romanMap[1](one) + romanMap[1](one), | |
3: one => romanMap[2](one) + romanMap[1](one), | |
4: (one, five) => romanMap[1](one) + `${five}`, | |
5: (one, five) => `${five}`, | |
6: (one, five) => romanMap[5](one, five) + romanMap[1](one), | |
7: (one, five) => romanMap[5](one, five) + romanMap[2](one), | |
8: (one, five) => romanMap[5](one, five) + romanMap[3](one), | |
9: (one, five, ten) => romanMap[1](one) + `${ten}` | |
} | |
return romanMap[number](one, five, ten) | |
} | |
return ('' + num) | |
.split('') | |
.reverse() | |
.reduce((acc, curr, ix) => { | |
return getOutput(curr, ...romans[ix]) + acc | |
}, '') | |
} | |
convertToRoman(997); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment