Skip to content

Instantly share code, notes, and snippets.

@Cycymomo
Last active December 20, 2015 04:29
Show Gist options
  • Save Cycymomo/6071684 to your computer and use it in GitHub Desktop.
Save Cycymomo/6071684 to your computer and use it in GitHub Desktop.
// 140bytes - http://www.developpez.net/forums/d1362763/webmasters-developpement-web/javascript/ludique-defis-code-en-tweet/
/* 97b: */
function deromanize(s){
for(l=s.length,a=b=0;l;a-=i<b?d:-d,b=i)i='IVXLCDM'.search(s[--l]),d=(i%2?5:1)+'e'+(i>>1);return a
}
/* 99b: */
function deromanize(s){
for(l=s.length,a=b=0;i=j=' IVXLCDM'.search(s[--l]);a+=i<b?-d:d,b=i)for(d=1;--j;d*=j%2?5:2);return a
}
/* Tests : */
console.log( deromanize("XII") + ' / ' + (deromanize("XII") === 12) );
console.log( deromanize("XLIX") + ' / ' + (deromanize("XLIX") === 49) );
console.log( deromanize("LXXIV") + ' / ' + (deromanize("LXXIV") === 74) );
console.log( deromanize("CCCLXXXIII") + ' / ' + (deromanize("CCCLXXXIII") === 383) );
console.log( deromanize("M") + ' / ' + (deromanize("M") === 1000) );
console.log( deromanize("MMMDCCCLXXXVIII") + ' / ' + (deromanize("MMMDCCCLXXXVIII") === 3888) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment