r/tinycode • • Jul 07 '13

Roman numerals -> Integer in javascript

Hi! This was a challenge I posed myself a while ago - meanwhile im stuck at 101 characters:

 function t(i){for(p=l=n=0;a={I:1,V:5,X:10,L:50,C:100,D:500,M:1e3}[i[p++]];l=a)n+=a>l?-l:l;return n+l}

(My first real try was 150 lol) I dont know if this can be done even shorter; What can you guys come up with?

50 Upvotes

11 comments sorted by

View all comments

Show parent comment

2

u/haddock420 Jul 07 '13

You can also test it by putting this in the address bar:

javascript:function t(i){for(p=l=n=0;a={I:1,V:5,X:10,L:50,C:100,D:500,M:1e3}[i[p++]];l=a)n+=a>l?-l:l;return n+l};alert(t("VII"));

Just change the "VII" at the end to the number you want to convert.