Find the single digit sum of alphabetical values of a string | Javascript program for the above approach ; Traverse the given string ; If character is an alphabet ; Stores the sum of order of values ; Find the score ; Find the single digit sum ; Return the resultant sum ; Driver Code
function findTheSum ( str ) { let alpha = [ ] ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( ( str [ i ] . charCodeAt ( 0 ) >= " " . charCodeAt ( 0 ) && str [ i ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) || ( str [ i ] . charCodeAt ( 0 ) >= " " . charCodeAt ( 0 ) && str [ i ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) ) alpha . push ( str [ i ] ) ; } let score = 0 , n = 0 ; for ( let i = 0 ; i < alpha . length ; i ++ ) { if ( alpha [ i ] . charCodeAt ( 0 ) >= " " . charCodeAt ( 0 ) && alpha [ i ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) score += alpha [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) + 1 ; else score += alpha [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) + 1 ; } while ( score > 0 n > 9 ) { if ( score == 0 ) { score = n ; n = 0 ; } n += score % 10 ; score = Math . floor ( score / 10 ) ; } return n ; } let S = " " ; document . write ( findTheSum ( S ) ) ;