erikpalla
2/28/2017 - 12:48 PM

Recursive fn

Recursive fn

function collatz(n, steps = 0) {
    if (n === 1) {
        return steps;
    }
    if (n % 2 === 0) {
        steps++;
        return collatz(n / 2, steps);
    }
    if (n % 2 !== 0) {
        steps++;
        return collatz(3 * n + 1, steps);
    }
}
console.log(collatz(11));

function fibonacci(n) {
    if (n === 0) {
        return 0;
    }
    if (n === 1) {
        return 1;
    }

    return fibonacci(n - 1) + fibonacci(n - 2);
}
console.log(fibonacci(14));

function factorial(n) {
    if (n === 1) {
        return 1;
    }
    return n * factorial(n - 1);
}
console.log(factorial(5));