Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Finding sum of sequence upto a specified accuracy using JavaScript
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 −
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
Advertisements
