Sum of alternating sign Squares of first N natural numbers | Function to calculate the alternating sign sum ; Variable to store the absolute sum ; Variable to store the sign ; Variable to store the resultant sum ; Driver code
function summation ( n ) { var abs_sum = n * ( n + 1 ) / 2 ; var sign = n + 1 % 2 == 0 ? 1 : - 1 ; var result_sum = sign * abs_sum ; return result_sum ; } var N = 2 ; document . write ( summation ( N ) ) ;