Write a function that accepts string as a parameter, returning evaluated value of expression in dice notation, including addition and multiplication.
To
JavaScript solution, 340 chars when compressed (no eval, supports prefixed multiplicator and suffixed addition):
function comp (s, m, n, f, a) {
m = parseInt( m );
if( isNaN( m ) ) m = 1;
n = parseInt( n );
if( isNaN( n ) ) n = 1;
f = parseInt( f );
a = typeof(a) == 'string' ? parseInt( a.replace(/\s/g, '') ) : 0;
if( isNaN( a ) ) a = 0;
var r = 0;
for( var i=0; i
Test code:
var test = ["3d6 + 12", "4*d12 + 3", "d100"];
for(var i in test)
alert( test[i] + ": " + parse(test[i]) );
Compressed version (pretty sure you can do shorter):
function c(s,m,n,f,a){m=parseInt(m);if(isNaN(m))m=1;n=parseInt(n);if(isNaN(n))n=1;f=parseInt(f);a=typeof(a)=='string'?parseInt(a.replace(/\s/g,'')):0;if(isNaN(a))a=0;var r=0;for(var i=0;i