Skip to content

Instantly share code, notes, and snippets.

@tomkp
Forked from 140bytes/LICENSE.txt
Created October 18, 2011 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomkp/1295056 to your computer and use it in GitHub Desktop.
Save tomkp/1295056 to your computer and use it in GitHub Desktop.
140byt.es -- Click ↑↑ fork ↑↑ to play!
function(r) {
var
numerals = { // initialise the roman numeral values
M:1000,
D:500,
C:100,
L:50,
X:10,
V:5,
I:1
},
curr, // current character's value
comp = 0, // comparator
total = 0, // ongoing total
i = r.length; // length of string
for (;i--;) { // iterate over array in reverse
curr = numerals[r[i]]; // set the current character value
total += // add to OR subtract from total...
(curr >= comp // depending on wether the current value is greater than the comparator
? curr : -curr);
comp = // set the comparator...
(curr > comp // to the greater of either the...
? curr : comp); // current or comparator
}
return total; // return the total
}
function(a){var b={M:1e3,D:500,C:100,L:50,X:10,V:5,I:1},c,d=0,e=0,f=a.length;for(;f--;)c=b[a[f]],e+=c>=d?c:-c,d=c>d?c:d;return e}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "romanNumeralConverter",
"description": "Convert from roman numerals to integer",
"keywords": [
"romannumerals",
"roman",
"numerals",
"converter"
]
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
var roman = function(a){var b={M:1e3,D:500,C:100,L:50,X:10,V:5,I:1},c,d=0,e=0,f=a.length;for(;f--;)c=b[a[f]],e+=c>=d?c:-c,d=c>d?c:d;return e}
console.info(1804, roman("MCCMIV"));
console.info(-1, roman("IIIIIIV"));
</script>
</head>
<body>
</body>
</html>
@jed
Copy link

jed commented Oct 18, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment