1 回答
TA贡献1757条经验 获得超8个赞
const fibonacci = function(input) {
let n = Number(input);
if (n === 1) {
return 1;
} else if (n < 1) {
return "OOPS";
} else if (n > 1) {
let array = new Array(n); // <---- Starting here
let filled = array.fill(1);
let reduced = filled.reduce((acc, _, i) => {
acc.push((i <=1) ? i : acc[i-2] + acc[i-1])
return acc;
},[]); // <- reduce is initialized with an array (new array),
return reduced[n - 1] + reduced[n - 2];
}
}
由于reduce是用新数组初始化的,因此该函数正在减少(将新值添加到新的初始化数组中)并返回相同的值。
这里的减速器是如何工作的 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
添加回答
举报