Find the sum of the ascii values of characters which are present at prime positions | Function that returns true if n is prime ; Function to return the sum of the ascii values of the characters which are present at prime positions ; To store the sum ; For every character ; If current position is prime then add the ASCII value of the character at the current position ; Driver code
function isPrime ( n ) { if ( n == 0 n == 1 ) return false ; for ( let i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } function sumAscii ( str , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( isPrime ( i + 1 ) ) sum += str . charCodeAt ( i ) ; } return sum ; } let str = " " ; let n = str . length ; document . write ( sumAscii ( str , n ) ) ;