Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mapper = {
- '': 0,
- 'I': 1,
- 'II': 2,
- 'III': 3,
- 'IV': 4,
- 'V': 5,
- 'VI': 6,
- 'VII': 7,
- 'VIII': 8,
- "IX": 9,
- 'X': 10,
- 'XX': 20,
- 'XXX': 30,
- 'XL': 40,
- 'L': 50,
- 'LX': 60,
- 'LXX': 70,
- 'LXXX': 80,
- 'XC': 90,
- 'C': 100,
- 'CC': 200,
- 'CCC': 300,
- 'CD': 400,
- 'D': 500,
- 'DC': 600,
- 'DCC': 700,
- 'DCCC': 800,
- 'CM': 900,
- 'M': 1000,
- 'MM': 2000,
- 'MMM': 3000
- }
- const fromRoman = (roman) => {
- const pattern = /^(MMM|MM|M)*(C|CC|CCC|CD|D|DC|DCC|DCCC|CM)?(X|XX|XXX|XL|L|LX|LXX|LXXX|XC)?(I|II|III|IV|V|VI|VII|VIII|IX)?$/gi;
- const arrayOfRoman = pattern.exec(roman);
- const [_, hilyadni, stotici, desetici, edinici] = [...arrayOfRoman]
- return [hilyadni, stotici, desetici, edinici].map(el => el ? el : '').reduce((result, el) => result += mapper[el], 0)
- }
- console.log(fromRoman('M'));
- console.log(fromRoman('CMXCIX'));
- console.log(fromRoman('IV'));
- console.log(fromRoman('I'));
- console.log(fromRoman('MCMXCI'));
- console.log(fromRoman('MMVI'));
- console.log(fromRoman('MMXX'));
- console.log(fromRoman('XXI'));
- console.log(fromRoman('I'));
- console.log(fromRoman("III"));
- console.log(fromRoman('IV'));
- console.log(fromRoman('MMVII'));
- console.log(fromRoman('MDCLXIX'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement