Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function toRoman(date)
- {
- const thousands = Math.floor(date / 1000);
- date %= 1000;
- const hundreds = Math.floor(date / 100);
- date %= 100;
- const tenths = Math.floor(date / 10);
- const singles = date % 10;
- const convert = function (digit, single, five, next) {
- if (digit == 0) {
- return '';
- }
- return digit == 9 ? single + next : digit == 4 ? single + five : ((digit > 4 ? five : '') + single.repeat(digit % 5));
- }
- let result = '';
- if (thousands > 0) {
- result += 'M'.repeat(thousands);
- }
- result += convert(hundreds, 'C', 'D', 'M');
- result += convert(tenths, 'X', 'L', 'C');
- result += convert(singles, 'I', 'V', 'X');
- return result;
- }
- function fromRoman(date)
- {
- const cost = {
- 'I': 1,
- 'V': 5,
- 'X': 10,
- 'L': 50,
- 'C': 100,
- 'D': 500,
- 'M': 1000
- };
- return date.toUpperCase().split('').reduce(function (accumulator, el, i, letters) {
- if (typeof cost[el] == "undefined") {
- return accumulator;
- }
- if (
- el == 'I' && 'VX'.indexOf(letters[i+1]) != - 1
- || el == 'X' && 'LC'.indexOf(letters[i+1]) != - 1
- || el == 'C' && 'DM'.indexOf(letters[i+1]) != - 1
- ) {
- return accumulator - cost[el];
- }
- return accumulator + cost[el];
- }, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement