Skip to content

Instantly share code, notes, and snippets.

@alenabdula
Created December 20, 2015 15:12
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 alenabdula/c59ffae8963164b3d970 to your computer and use it in GitHub Desktop.
Save alenabdula/c59ffae8963164b3d970 to your computer and use it in GitHub Desktop.
function convert( num ) {
var x = '';
[1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1].map(
function( obj, i ) {
while ( num >= obj ) {
x += ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'][i];
num -= obj;
}
}
);
return x;
}
console.log( convert( 21 ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment