Last active
May 12, 2017 00:34
-
-
Save casmith/01231ebc55945806cc73f5e9e35adde7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
'use strict'; | |
function convertDigit(val, one, five, ten) { | |
if (val !== '' && val !== undefined && val !== '0') { | |
val = parseInt(val); | |
if (val < 4) { | |
return one.repeat(val); | |
} else if (val <= 5) { | |
return one.repeat(5 - val) + five; | |
} else if (val < 9) { | |
return five + one.repeat(val - 5); | |
} else { | |
return one.repeat(10 - val) + ten; | |
} | |
} | |
return ''; | |
} | |
module.exports = { | |
toRoman: function (val) { | |
val = ('' + val).split('').reverse().join(''); | |
return convertDigit(val[2], 'C', 'D', 'M') + | |
convertDigit(val[1], 'X', 'L', 'C') + | |
convertDigit(val[0], 'I', 'V', 'X') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment