Skip to content

Instantly share code, notes, and snippets.

@tgraham777
Created December 15, 2015 17:56
Show Gist options
  • Select an option

  • Save tgraham777/665fa7319fa786537e43 to your computer and use it in GitHub Desktop.

Select an option

Save tgraham777/665fa7319fa786537e43 to your computer and use it in GitHub Desktop.
Remote day work - recursion and generators in Javascript
function countdown(n) {
if (n >= 1) {
console.log(n);
countdown(n-1);
}
}
countdown(5);
var i = 2;
var fib = [1, 1];
function fibonacci(n) {
if (i < n) {
fib[i] = fib[i-2] + fib[i-1];
i++;
fibonacci(n);
} else {
console.log(fib);
}
}
fibonacci(10);
function* factorialGenerator() {
var count = 1;
var i = 1;
while (true) {
if (count > 0) {
i = i * count;
count++;
}
yield i;
}
}
var factorial = factorialGenerator();
console.log(factorial.next().value);
console.log(factorial.next().value);
console.log(factorial.next().value);
console.log(factorial.next().value);
console.log(factorial.next().value);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment