斐波那契数列
斐波那契数列:0 1 1 2 3 5 8 13 21...
给出一个n,求数组。
代码如下:
/** * Calculate fibonacci number at specific position using Dynamic Programming approach. * * @param n * @return {number} */ export default function fibonacciNth(n) { let currentValue = 1; let previousValue = 0; if (n === 1) { return 1; } let iterationsCounter = n - 1; while (iterationsCounter) { currentValue += previousValue;//当前要放入数组的数 previousValue = currentValue - previousValue;//当前要放入数组的前一个数 iterationsCounter -= 1; } return currentValue; }