Last active
April 29, 2017 22:18
-
-
Save thomascothran/51aa8fa9ac3d99aa00c44c37b31b18b2 to your computer and use it in GitHub Desktop.
Roman Numerals to Arabic Numerals with Tail Calls
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 nums = {I: 1, V: 5, X: 10, L: 50, C: 100, M: 1000}; | |
| const arabify2 = (romNum, sum=0) => { | |
| const [fst, snd, rest] = [romNum[0], romNum[1], romNum.slice(2)]; | |
| if (!snd) {return nums[fst] ? nums[fst] + sum : sum;} | |
| else if (nums[snd] > nums[fst]) { | |
| return arabify2(rest, nums[snd] - nums[fst] + sum); | |
| } else {return arabify2(snd + rest, nums[fst] + sum);} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment