Program to find the sum of the series 1 + x + x ^ 2 + x ^ 3 + . . + x ^ n | Function to find the sum of the series and print N terms of the given series ; First Term ; Loop to print the N terms of the series and find their sum ; Driver code
function sum ( x , n ) { let i , total = 1.0 , multi = x ; document . write ( total + " " ) ; for ( i = 1 ; i < n ; i ++ ) { total = total + multi ; document . write ( multi + " " ) ; multi = multi * x ; } document . write ( " " ) ; return total ; } let x = 2 ; let n = 5 ; document . write ( sum ( x , n ) . toFixed ( 2 ) ) ;