Created
January 29, 2017 03:32
-
-
Save johncmunson/aae85ee59817d3e5ab37006f4b9b0310 to your computer and use it in GitHub Desktop.
Accepts a number and returns the corresponding roman numeral.
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
var romanize = function(num) { | |
var rom = { | |
M: 1000, | |
CM: 900, | |
D: 500, | |
CD: 400, | |
C: 100, | |
XC: 90, | |
L: 50, | |
XL: 40, | |
X: 10, | |
IX: 9, | |
V: 5, | |
IV: 4, | |
I: 1 | |
}; | |
return Object.keys(rom).reduce(function(acc, ch) { | |
acc.str += ch.repeat(acc.num / rom[ch]); | |
acc.num %= rom[ch]; | |
return acc; | |
}, { | |
str: '', | |
num: num | |
}).str; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment