Median | Function for calculating median ; First we sort the array ; check for even case ; Driver program
function findMedian ( a , n ) { a . sort ( ) ; if ( n % 2 != 0 ) return a [ parseInt ( n / 2 ) ] ; return ( a [ parseInt ( ( n - 1 ) / 2 ) ] + a [ parseInt ( n / 2 ) ] ) / 2.0 ; } let a = [ 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 ] ; let n = a . length ; document . write ( " " + findMedian ( a , n ) ) ;