Find sum in range L to R in given sequence of integers | Function to find the sum within the given range ; generating array from given sequence ; calculate the desired sum ; return the sum ; Initialise the range
function findSum ( L , R ) { var arr = [ ] ; var i = 0 ; var x = 2 ; while ( i <= R ) { arr . push ( i + x ) ; if ( i + 1 <= R ) arr . push ( i + 1 + x ) ; x *= - 1 ; i += 2 ; } var sum = 0 ; for ( var i = L ; i <= R ; ++ i ) sum += arr [ i ] ; return sum ; } var L = 0 , R = 5 ; document . write ( findSum ( L , R ) ) ;