Program for Mean and median of an unsorted array | Function for calculating mean ; Function for calculating median ; First we sort the array ; check for even case ; Driver Code ; Function call
function findMean ( a , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += a [ i ] ; return sum / n ; } function findMedian ( a , n ) { a . sort ( ) ; if ( n % 2 != 0 ) return a [ n / 2 ] ; return ( a [ Math . floor ( ( n - 1 ) / 2 ) ] + a [ n / 2 ] ) / 2 ; } let a = [ 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 ] let n = a . length ; document . write ( " " + findMean ( a , n ) + " " ) ; document . write ( " " + findMedian ( a , n ) ) ;