RickBacci
12/15/2015 - 7:21 PM

## Countdown with recursion

Countdown with recursion

``````function fibonacci(length, sequence) {

var fibSequence       = sequence || [1, 1];
var fibSequenceLength = fibSequence.length;

if        (length === 0) { console.log( fibSequence );
} else if (length === 1) { console.log( [1]         );
} else if (length === 2) { console.log( fibSequence );
} else {
var nextFibNum =
fibSequence[fibSequence.length - 2] +                            fibSequence[fibSequence.length - 1];

fibSequence.push(nextFibNum);

fibonacci(length - 1, fibSequence);
}

}

fibonacci(1);
fibonacci(2);
fibonacci(3);
fibonacci(4);
fibonacci(10);``````
``````function* factorialGenerator() {
var total = 0;
var num   = 1;

while(true) {
total += num;
num   += 1;
yield total;

}

}

var factorial = factorialGenerator();

console.log(factorial.next().value); // 1
console.log(factorial.next().value); // 2
console.log(factorial.next().value); // 6
console.log(factorial.next().value); // 10
console.log(factorial.next().value); // 15
``````
``````function countdown(number) {
if (number !== 0) {
console.log(number);
countdown(number - 1);
} else {
console.log(0);
}
}

countdown(5);
``````