Skip to content

Instantly share code, notes, and snippets.

@johncmunson
Created January 29, 2017 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johncmunson/aae85ee59817d3e5ab37006f4b9b0310 to your computer and use it in GitHub Desktop.
Save johncmunson/aae85ee59817d3e5ab37006f4b9b0310 to your computer and use it in GitHub Desktop.
Accepts a number and returns the corresponding roman numeral.
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