我正在尝试创建斐波那契发生器。也许smb可以向我解释我如何不能从零开始创建序列,但是起点将根据用户请求(例如序列中的数字5)并且可以前进和后退(也为负)。显示数量也应根据用户要求。
function* fibonacci(number) {
let previous_first = 0, previous_second = 1, next = 1;
while(true) {
next = previous_first + previous_second;
previous_first = previous_second;
previous_second = next;
yield next;
}
};
答案 0 :(得分:1)
如下所示的结果:
function getFibonnaciSequence(firstNum, secondNum, sequenceCount) {
let sum = 0;
let squenceArray = [];
while(sequenceCount != 0) {
sum = firstNum + secondNum;
squenceArray.push(sum);
firstNum = secondNum;
secondNum = sum;
sequenceCount--;
}
return squenceArray;
}
console.log(getFibonnaciSequence(2,5, 10));