Merge pull request #409 from Luislev/main

10_fibonacci: Add alternative solution in fibonacci-solution.js
This commit is contained in:
Eric Olkowski 2023-11-11 07:17:33 -05:00 committed by GitHub
commit a3992aa0de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 1 deletions

View File

@ -4,7 +4,7 @@ const fibonacci = function(count) {
let firstPrev = 1; let firstPrev = 1;
let secondPrev = 0; let secondPrev = 0;
for (let i = 2; i <= count; i++) { for (let i = 2; i <= count; i++) {
let current = firstPrev + secondPrev; let current = firstPrev + secondPrev;
secondPrev = firstPrev; secondPrev = firstPrev;
@ -12,6 +12,14 @@ const fibonacci = function(count) {
} }
return firstPrev; return firstPrev;
}; };
// Another way to do it is by using an iterative approach with an array containing two values, 0 and 1.
// const fib = [0, 1];
// for (let i = 2; i <= count; i++) {
// fib[i] = fib[i - 1] + fib[i - 2];
// }
// return fib[count];
module.exports = fibonacci; module.exports = fibonacci;