Created
April 25, 2017 17:15
-
-
Save andrewhl/fcc4b4e52586b323c0939a1c0be2d56e 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
//jshint esversion:6 | |
function convertToRoman(num) { | |
const UNITS = { | |
1: 'I', | |
4: 'IV', | |
5: 'V', | |
9: 'IX', | |
10: 'X', | |
40: 'XL', | |
50: 'L', | |
90: 'XC', | |
100: 'C', | |
400: 'CD', | |
500: 'D', | |
900: 'CM', | |
1000: 'M' | |
}; | |
if (num === 0) { | |
return ''; | |
} | |
let value = Object.keys(UNITS).map(i => parseInt(i, 10)).reduce((a, b) => { | |
if (b <= num) { | |
return b; | |
} | |
return a; | |
}, 0); | |
return UNITS[value] + convertToRoman(num - value); | |
} | |
convertToRoman(36); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment