Find the sum of the first half and second half elements of an array | Function to find the sum of the first half elements and second half elements of an array ; Add elements in first half sum ; Add elements in the second half sum ; Driver code ; Function call
function sum_of_elements ( arr , n ) { var sumfirst = 0 , sumsecond = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( i < parseInt ( n / 2 ) ) { sumfirst += arr [ i ] ; } else { sumsecond += arr [ i ] ; } } document . write ( " " + sumfirst + " " ) ; document . write ( " " + sumsecond + " " ) ; } var arr = [ 20 , 30 , 60 , 10 , 25 , 15 , 40 ] ; var n = arr . length ; sum_of_elements ( arr , n ) ;