Skip to content

Instantly share code, notes, and snippets.

@vampireneo
Created May 31, 2016 07:50
Show Gist options
  • Save vampireneo/f98e1e76ff07e12e4cc54773d93fa2d4 to your computer and use it in GitHub Desktop.
Save vampireneo/f98e1e76ff07e12e4cc54773d93fa2d4 to your computer and use it in GitHub Desktop.
Convert a number to Roman Numerals
function solution(roman){
let RomanNum = {
10: { 1: 'I', 5: 'V'},
100: { 1: 'X', 5: 'L'},
1000: { 1: 'C', 5: 'D'},
10000: { 1: 'M'}
};
let result = '';
for (let d in RomanNum) {
let tmp = '';
let n = roman % d;
roman -= n;
console.log(n);
n /= (d/10);
if (n === 9) {
tmp = RomanNum[d][1]+RomanNum[d*10][1];
n = 0;
}
if (n >= 5) {
tmp = RomanNum[d][5];
n -= 5;
}
if (n === 4) {
tmp = RomanNum[d][1] + RomanNum[d][5];
n = 0;
}
while(n-- > 0) {
tmp += RomanNum[d][1];
}
result = tmp + result;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment