Sum of numbers formed by consecutive digits present in a given string | Function to calculate the sum of numbers formed by consecutive sequences of digits present in the string ; Stores consecutive digits present in the string ; Stores the sum ; Iterate over characters of the input string ; If current character is a digit ; Append current digit to curr ; Add curr to sum ; Reset curr ; Driver Code
function sumOfDigits ( s ) { var curr = 0 ; var ret = 0 ; s . split ( ' ' ) . forEach ( ch => { if ( parseInt ( ch ) ) { curr = curr * 10 + ch . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; } else { ret += curr ; curr = 0 ; } } ) ; ret += curr ; return ret ; } var S = " " ; document . write ( sumOfDigits ( S ) ) ;