New Solution
A different solution to the fibonacci excercise
This commit is contained in:
parent
9a40c04726
commit
8f41ff4667
|
@ -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;
|
||||||
|
|
Loading…
Reference in New Issue