New Solution

A different solution to the fibonacci excercise
This commit is contained in:
Madihah 2021-05-20 17:24:31 +01:00 committed by GitHub
parent 9a40c04726
commit 8f41ff4667
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 10 deletions

View File

@ -1,14 +1,23 @@
const fibonacci = function(count) { const fibonacci = function (member) {
if (count < 0) return "OOPS"; let indexNo = parseInt(member) - 1;
if (count == 0) return 0; if (indexNo < 0) {
let a = 0; return "OOPS";
let b = 1;
for (let i = 1; i < count; i++) {
const temp = b;
b = a + b;
a = temp;
} }
return b;
let fib = [1, 1];
function makeFib() {
let lastGuy = fib[fib.length - 1];
let secondLast = fib[fib.length - 2];
let newGuy = lastGuy + secondLast;
fib.push(newGuy);
}
while (fib.length <= indexNo) {
makeFib();
}
return fib[indexNo];
}; };
module.exports = fibonacci; module.exports = fibonacci;