Geometric mean ( Two Methods ) | function to calculate geometric mean and return float value . ; declare sum variable and initialize it to 1. ; Compute the sum of all the elements in the array . ; compute geometric mean through formula antilog ( ( ( log ( 1 ) + log ( 2 ) + . . . + log ( n ) ) / n ) and return the value to main function . ; Driver function ; function call
function geometricMean ( arr , n ) { var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) sum = sum + Math . log ( arr [ i ] ) ; sum = sum / n ; return Math . exp ( sum ) ; } var arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ; var n = arr . length ; document . write ( geometricMean ( arr , n ) . toFixed ( 5 ) ) ;