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