|
function( |
|
a // string |
|
/* I just like when function's length property return something meaningful */ |
|
){ |
|
// Processing each char of `a` starting from the end of string: |
|
for( |
|
// Define the variables: |
|
// v - value. To be assigned later |
|
// s - sum |
|
// i - iterator, length of the arg |
|
var v, s = 0, i = a.length |
|
; |
|
// decrease iterator with post-decrement |
|
i-- |
|
; |
|
// add the following to sum: |
|
s += |
|
// stored is more than following expression... |
|
v > ( |
|
// ...where current value is re-assigned. |
|
// Here we define an array using elision |
|
// and right after that get its value on key that is an integer: |
|
v = [100,500,,,,,1,,,50,1e3,,,,,,,,,5,,10][ |
|
// Get the char at the `i` offset and treat it as it was an integer in 36-based notation. |
|
// The good thing: we don't need to use lo-ong toUpperCase :) |
|
// And then decrease it by 12. This makes the array declaration slightly shorter |
|
parseInt(a.charAt(i), 36) - 12 |
|
] |
|
// Here we get a positive value if char was one of: "IVXLCDMivxlcdm" and undefined else |
|
// Using the subtraction check if this value is less than previous |
|
// (Considering the fact we're parsing string from the tail, this is not the previous but *next* char) |
|
// If there was no previous value, the whole subtraction gives NaN and comparation gives false |
|
) ? |
|
// If yes (i.e. IX or CM) we should subtract the current value from the sum |
|
-v |
|
: |
|
// If no (i.e. XX or L{end-of-string}) we should add the current value to the sum |
|
// If `v` is a NaN here, the sum will be NaN either. Garbage In Garbage Out, hehe. |
|
// Uncomment ~~ if you don't like this behavior, there's still bytes left. |
|
/* ~~ */ v |
|
) |
|
/* empty loop body */ |
|
; |
|
// return the sum |
|
return s |
|
} |
@williammalo Oh... In your version
ileaks into outer scope