Skip to content

Instantly share code, notes, and snippets.

@lfelguetac
Created October 18, 2021 22:55
Show Gist options
  • Save lfelguetac/639afbdf242b1cae9bea871f21e632cb to your computer and use it in GitHub Desktop.
Save lfelguetac/639afbdf242b1cae9bea871f21e632cb to your computer and use it in GitHub Desktop.
secuencia fibonacci
const getFibonnaci = (n: number) => {
let fibonnaci = [0, 1];
let large = fibonnaci.length;
while (true) {
let nextValue = fibonnaci[large-2] + fibonnaci[large-1]
if (nextValue > n) break
fibonnaci.push(nextValue);
large = fibonnaci.length;
}
return fibonnaci;
}
@lfelguetac
Copy link
Author

a best idea ?

@lfelguetac
Copy link
Author

lfelguetac commented Oct 19, 2021

mejor idea:

const getFibonnaci = (n: number, fibonnaci: number[] = [0]): number[] => {
    let large = fibonnaci.length;
    let nextValue = fibonnaci[large-2] + fibonnaci[large-1]
    if (nextValue > n) return fibonnaci;
    else {
        return getFibonnaci(n, [...fibonnaci, !nextValue ? 1 : nextValue])
    }
}

const fibo = getFibonnaci(100);
.... 0, 1, 1, 2, 3,
5, 8, 13, 21, 34,
55, 89

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment