avatar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
let fibTable = [0, 1];
const initFib = (size) => {
for (let i = 2; i < size; i++) {
fibTable.push(fibTable[i - 1] + fibTable[i - 2]);
}
};

const getFib = (n) => {
if (n < fibTable.length) {
return fibTable[n];
} else {
for (let i = fibTable.length; i <= n; i++) {
fibTable.push(fibTable[i - 1] + fibTable[i - 2]);
}
return fibTable[n];
}
};

initFib(10);


Comment