Finding sum of sequence upto a specified accuracy using JavaScript

JavascriptWeb DevelopmentFront End Technology

Problem

Suppose the following sequence:

Seq: 1/1 , 1/1x2 , 1/1x2x3 , 1/1x2x3x4 , ....

The nth term of this sequence will be −

1 / 1*2*3 * ... n

We are required to write a JavaScript function that takes in a number n, and return the sum of first n terms of this sequence.

Example

Following is the code −

 Live Demo

const num = 12;
const seriesSum = (num = 1) => {
   let m = 1;
   let n = 1;
   for(let i = 2; i < num + 1; i++){
      m *= i;
      n += (m * -1);
   };
   return n;
};
console.log(seriesSum(num));

Output

-522956311
raja
Published on 17-Apr-2021 13:37:26
Advertisements