JavaScript代码用于查找级数的第n项 - 等差数列 (AP)


我们需要编写一个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

更新于:2020年9月15日

921 次浏览

开启您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.