Created
July 27, 2023 23:53
-
-
Save segdeha/33a1f594839305a16af16b84d0e4b417 to your computer and use it in GitHub Desktop.
Bookmarklet to quickly return the decimal version of any Roman numeral
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
javascript:(function romans(){const romans={I:1,V:5,X:10,L:50,C:100,D:500,M:1000};function getDecimalFromRomanNumeral(romanNumeral){let decimal;decimal=0;for (let i=0,l=romanNumeral.length;i<l;++i) {if(romanNumeral[i+1]&&romans[romanNumeral[i]]<romans[romanNumeral[i+1]]){decimal+=romans[romanNumeral[i+1]]-romans[romanNumeral[i]];++i;}else{decimal+=romans[romanNumeral[i]];}}return decimal;}const input=prompt('Roman numerals');const result=getDecimalFromRomanNumeral(input.toUpperCase());alert(result);})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment