Alternate Primes till N | * Function for checking number is prime or not ; if flag = 0 then number is prime and return 1 otherwise return 0 ; Function for printing alternate prime number ; counter is initialize with 0 ; looping through 2 to n - 1 ; function calling along with if condition ; if counter is multiple of 2 then only print prime number ; Driver code ; Function calling
function prime ( num ) { var i , flag = 0 ; for ( i = 2 ; i <= num / 2 ; i ++ ) { if ( num % i == 0 ) { flag = 1 ; break ; } } if ( flag == 0 ) return 1 ; else return 0 ; } function print_alternate_prime ( n ) { var counter = 0 ; for ( num = 2 ; num < n ; num ++ ) { if ( prime ( num ) == 1 ) { if ( counter % 2 == 0 ) document . write ( num + " " ) ; counter ++ ; } } } var n = 15 ; document . write ( " " + " " + " " + n + " " ) ; print_alternate_prime ( n ) ;