Last active
June 15, 2020 13:08
-
-
Save codejockie/d15d00a5834dcf484e49e9d79844a129 to your computer and use it in GitHub Desktop.
Roman numerals to Arabic numerals
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
| const romanMap: { [key: string]: number } = { | |
| I: 1, | |
| V: 5, | |
| X: 10, | |
| L: 50, | |
| C: 100, | |
| D: 500, | |
| M: 1000, | |
| } | |
| export function romanArabic(roman: string): number { | |
| const chars = roman.toUpperCase().split("") | |
| return chars.reduce((acc: number, char: string, i: number) => { | |
| const previousChar = chars[i - 1] | |
| const isGreater = romanMap[char] > romanMap[previousChar] | |
| return isGreater | |
| ? acc - | |
| romanMap[previousChar] + | |
| (romanMap[char] - romanMap[previousChar]) | |
| : acc + romanMap[char] | |
| }, 0) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another method