Created
September 4, 2018 21:25
-
-
Save stevenYouhana/fec4b7acae073a34dd7eaa21bac38929 to your computer and use it in GitHub Desktop.
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
function convertToRoman(num) { | |
const BASE = { | |
'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 | |
} | |
function quotient(divident, divisor) { | |
return divident/divisor; | |
} | |
function getBase(n) { | |
let ret = ''; | |
let matchKey = 4; | |
for(let b in BASE) { | |
if(n >= BASE[b]) { | |
matchKey = b; | |
break; | |
} | |
} | |
for(let i=1; i<=quotient(n,BASE[matchKey]); i++) { | |
ret += matchKey; //number of times | |
} | |
num = n%BASE[matchKey]; //update num | |
return ret; | |
} | |
let roman = ''; | |
while(num > 0) { | |
roman += getBase(num); | |
} | |
return roman; | |
} | |
convertToRoman(408)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment