-
-
Save tomkp/1295056 to your computer and use it in GitHub Desktop.
140byt.es -- Click ↑↑ fork ↑↑ to play!
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
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 | |
} |
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
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} |
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
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. |
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
{ | |
"name": "romanNumeralConverter", | |
"description": "Convert from roman numerals to integer", | |
"keywords": [ | |
"romannumerals", | |
"roman", | |
"numerals", | |
"converter" | |
] | |
} |
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
<!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> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@subzey beat you to it: https://gist.github.com/1040240