- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements