Count strings having sum of ASCII values of characters equal to a Prime or Armstrong Number | Function to check if a number is prime number ; Define a flag variable ; Check for factors of num ; If factor is found , set flag to True and break out of loop ; Check if flag is True ; Function to calculate order of the number x ; Function to check whether the given number is Armstrong number or not ; If the condition satisfies ; Function to count Armstrong valued strings ; Stores the count of Armstrong valued strings ; Iterate over the list ; Store the value of the string ; Find value of the string ; Check if it an Armstrong number ; Function to count prime valued strings ; Store the count of prime valued strings ; Iterate over the list ; Store the value of the string ; Find value of the string ; Check if it is a Prime Number ; Driver code ; Function Call
function isPrime ( num ) { let flag = false ; if ( num > 1 ) { for ( let i = 2 ; i < num ; i ++ ) { if ( ( num % i ) == 0 ) { flag = true ; break ; } } } if ( flag ) return false ; else return true ; } function order ( x ) { let n = 0 ; while ( x != 0 ) { n = n + 1 ; x = x / 10 ; } return n ; } function isArmstrong ( x ) { let n = order ( x ) ; let temp = x ; let sum1 = 0 ; while ( temp != 0 ) { let r = temp % 10 ; sum1 = sum1 + Math . pow ( r , n ) ; temp = temp / 10 ; } return ( sum1 == x ) ; } function count_armstrong ( li ) { let c = 0 ; for ( let ele of li ) { let val = 0 ; for ( let che of ele ) val += che . charCodeAt ( 0 ) ; if ( isArmstrong ( val ) ) c += 1 ; } return c ; } function count_prime ( li ) { let c = 0 ; for ( let ele of li ) { let val = 0 ; for ( let che of ele ) val += che . charCodeAt ( 0 ) ; if ( isPrime ( val ) ) c += 1 ; } return c ; } let arr = [ " " , " " , " " , " " , " " , " " , " " ] ; document . write ( " " + count_armstrong ( arr ) + " " ) ; document . write ( " " + count_prime ( arr ) + " " ) ;