Expectation or expected value of an array | Function to calculate expectation ; Variable prb is for probability of each element which is same for each element ; calculating expectation overall ; returning expectation as sum ; driver function ; Function for calculating expectation ; Display expectation of given array
function calc_Expectation ( a , n ) { let prb = ( 1 / n ) ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += a [ i ] * prb ; return sum ; } let expect , n = 6 ; let a = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; expect = calc_Expectation ( a , n ) ; document . write ( " " + expect ) ;