how about this?
nums = "12345";
function sumString( s ) {
sum = 0;
for( i = 0; i < s.length; i++ ) {
num = parseInt( s.charAt( i ) );
if( ! isNan( num ) )
sum += num;
}
return sum;
}
trace( sumString( nums ) );
::EDIT::
…or to go minimal…
function sumString( s ) {
for( sum=0,i = 0; i < s.length; sum += ! isNan( s.charAt( i ) ) ? parseInt( s.charAt( i ) ) : 0, i++ );
return sum;
}
::EDIT2::
…or…
function sumString( s ) {
for( sum = 0; s.length > 0; sum += ! isNan( s.slice(-1) ) ? parseInt( s.slice(-1) ) : 0, s = s.slice( 0, -1 ) );
return sum;
}
::EDIT3::
…or…
function sumString( s ) {
return 0 + isNan( s.slice(-1) ) ? 0 : s.length > 1 ? parseInt(s.slice(-1)) + sumString(s.slice(0,-1)) : parseInt(s.slice(-1));
}
the last one will stop on the first non-numeric (NaN) input… could be fixed by nesting checks but seems unnecessary… and it processes the string from end to start in difference from the others… plus it is useless for huge strings, since I noticed when developing a game which used a recursive function, flash didn’t really like more than 255 (I think) recursive calls… Didn’t cause a stack overflow though. Maybe there is a workaround…