用于查找系列的第 n 项的 JavaScript 代码 - 算术级数 (AP)

javascriptweb developmentfront end technologyobject oriented programming更新于 2025/12/5 10:52:17

我们需要编写一个 JavaScript 函数,该函数接受三个数字作为参数(前两个数字应该是算术级数的开始两个连续项)。

第三个数字,即 n,是系列中基于 1 索引的元素,我们需要计算其值

例如 −

如果输入为 2、5、7

那么系列将是 −

2、5、8、11、14、17、20

输出应该是 20。

示例

以下是代码 −

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));

输出

以下是控制台中的输出 −

20

相关文章


有用资源