Skip to content

Instantly share code, notes, and snippets.

@alaq
Created June 8, 2017 19:52
Show Gist options
  • Save alaq/c795234c52f2e667eaa193754773cc21 to your computer and use it in GitHub Desktop.
Save alaq/c795234c52f2e667eaa193754773cc21 to your computer and use it in GitHub Desktop.
function arabicToRomanNumerals(number){
function stringMaker(times, letter, higherLetter, matchingNumber){
var str = '';
if (times < 5 || letter === 'M') for (var i = 0 ; i < times ; i++) str += letter;
else if (times === 4) str = letter;
return str;
}
var s = [];
s[0] = stringMaker(parseInt (number / 1000), 'M');
s[1] = stringMaker(parseInt (number % 1000 / 900), 'CM');
s[2] = stringMaker(parseInt (number % 1000 % 900 / 500), 'D');
s[3] = stringMaker(parseInt (number % 1000 % 900 % 500 / 400), 'CD');
s[4] = stringMaker(parseInt (number % 1000 % 900 % 500 % 400 / 100), 'C');
s[5] = stringMaker(parseInt (number % 1000 % 900 % 500 % 400 % 100 / 90), 'XC');
s[6] = stringMaker(parseInt (number % 1000 % 900 % 500 % 400 % 100 % 90 / 50), 'L');
s[7] = stringMaker(parseInt (number % 1000 % 900 % 500 % 400 % 100 % 90 % 50 / 40), 'XL');
s[8] = stringMaker(parseInt (number % 1000 % 900 % 500 % 400 % 100 % 90 % 50 % 40 / 10), 'X');
s[9] = stringMaker(parseInt (number % 1000 % 900 % 500 % 400 % 100 % 90 % 50 % 40 % 10 / 9), 'IX');
s[10] = stringMaker(parseInt (number % 1000 % 900 % 500 % 400 % 100 % 90 % 50 % 40 % 10 % 9 / 5), 'V');
s[11] = stringMaker(parseInt (number % 1000 % 900 % 500 % 400 % 100 % 90 % 50 % 40 % 10 % 9 % 5 / 4), 'IV');
s[12] = stringMaker(parseInt (number % 1000 % 900 % 500 % 400 % 100 % 90 % 50 % 40 % 10 % 9 % 5 % 4), 'I');
s = s.join('');
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment