Skip to content

Instantly share code, notes, and snippets.

@thomascothran
Last active April 29, 2017 22:18
Show Gist options
  • Save thomascothran/51aa8fa9ac3d99aa00c44c37b31b18b2 to your computer and use it in GitHub Desktop.
Save thomascothran/51aa8fa9ac3d99aa00c44c37b31b18b2 to your computer and use it in GitHub Desktop.
Roman Numerals to Arabic Numerals with Tail Calls
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