Skip to content

Instantly share code, notes, and snippets.

@rickbacci
Last active December 15, 2015 19:28
Show Gist options
  • Save rickbacci/4a15d678fb47147ed443 to your computer and use it in GitHub Desktop.
Save rickbacci/4a15d678fb47147ed443 to your computer and use it in GitHub Desktop.
Countdown with recursion
function countdown(number) {
if (number !== 0) {
console.log(number);
countdown(number - 1);
} else {
console.log(0);
}
}
countdown(5);
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 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment