JavaScript code to find nth term of a series - Arithmetic Progression (AP)


We are required to write a JavaScript function that takes in three numbers as argument (first two numbers are supposed to be the starting two consecutive terms of an arithmetic progression).

And the third number, say n, is the 1 index-based element of the series whose value we are required to calculate

For example −

If the input is 2, 5, 7

Then the series will be −

2, 5, 8, 11, 14, 17, 20

And the output should be 20.

Example

Following is the code −

const a = 2, b = 5;
const N = 7;
const findNthTerm = (first, second, num) => {
   const diff = second - first;
   const fact = (num - 1) * diff;
   const term = first + fact;
   return term;
};
console.log(findNthTerm(a, b, N));

Output

Following is the output in the console −

20

Updated on: 15-Sep-2020

867 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements