Average of a stream of numbers | javascript program to return Average of a stream of numbers ; Returns the new average after including x ; Prints average of a stream of numbers ; Driver Code
var sum = 0 , n = 0 ; function getAvg ( x ) { sum += x ; n ++ ; return ( sum / n ) ; } function streamAvg ( arr , m ) { var avg = 0 ; for ( i = 0 ; i < m ; i ++ ) { avg = getAvg ( parseInt ( arr [ i ] ) ) ; document . write ( " " + ( i + 1 ) + " " + avg . toFixed ( 1 ) + " " ) ; } return ; } var arr = [ 10 , 20 , 30 , 40 , 50 , 60 ] ; var m = arr . length ; streamAvg ( arr , m ) ;