Skip to content

Instantly share code, notes, and snippets.

@casmith
Last active May 12, 2017 00:34
Show Gist options
  • Save casmith/01231ebc55945806cc73f5e9e35adde7 to your computer and use it in GitHub Desktop.
Save casmith/01231ebc55945806cc73f5e9e35adde7 to your computer and use it in GitHub Desktop.
'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