Average of even numbers till a given even number | Function to calculate the average of even numbers ; count even numbers ; store the sum of even numbers ; driver function
function averageEven ( n ) { if ( n % 2 != 0 ) { document . write ( " " ) ; return - 1 ; } let sum = 0 , count = 0 ; while ( n >= 2 ) { count ++ ; sum += n ; n = n - 2 ; } return sum / count ; } let n = 16 ; document . write ( averageEven ( n ) ) ;