Evaluate an array expression with numbers , + and | Function to find the sum of given array ; if string is empty ; parseInt function to convert string into integer ; parseInt function to convert string into integer ; Find operator ; If operator is equal to ' + ' , add value in sum variable else subtract ; Driver code
function calculateSum ( arr , n ) { if ( n == 0 ) return 0 ; let s = arr [ 0 ] ; let value = parseInt ( s ) ; let sum = value ; for ( let i = 2 ; i < n ; i = i + 2 ) { s = arr [ i ] ; value = parseInt ( s ) ; let operation = arr [ i - 1 ] [ 0 ] ; if ( operation == ' ' ) sum += value ; else sum -= value ; } return sum ; } let arr = [ " " , " " , " " , " " , " " , " " , " " ] ; let n = arr . length ; document . write ( calculateSum ( arr , n ) ) ;