Skip to content

Instantly share code, notes, and snippets.

@George-Hudson
Created December 15, 2015 18:26
Show Gist options
  • Save George-Hudson/043d172510fb4a741636 to your computer and use it in GitHub Desktop.
Save George-Hudson/043d172510fb4a741636 to your computer and use it in GitHub Desktop.
Recursion and Generators
<!DOCTYPE html>
<!--
Created using JS Bin
http://jsbin.com
Copyright (c) 2015 by anonymous (http://jsbin.com/ducanemoro/1/edit)
Released under the MIT license: http://jsbin.mit-license.org
-->
<meta name="robots" content="noindex">
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
function countdown(n) {
if (n <= 0) {
console.log(0);
} else {
console.log(n);
countdown(n - 1);
}
}
countdown(5);
</script>
</body>
</html>
<!DOCTYPE html>
<!--
Created using JS Bin
http://jsbin.com
Copyright (c) 2015 by anonymous (http://jsbin.com/qezuzivaxi/3/edit)
Released under the MIT license: http://jsbin.mit-license.org
-->
<meta name="robots" content="noindex">
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
function* factorialGenerator() {
var factorial = 1
var pastFactorials = 1
while (true) {
yield pastFactorials = pastFactorials * factorial++;
}
};
var generator = factorialGenerator();
console.log(generator.next());
console.log(generator.next());
console.log(generator.next());
console.log(generator.next());
console.log(generator.next());
</script>
</body>
</html>
<!DOCTYPE html>
<!--
Created using JS Bin
http://jsbin.com
Copyright (c) 2015 by anonymous (http://jsbin.com/xisiqexaxi/2/edit)
Released under the MIT license: http://jsbin.mit-license.org
-->
<meta name="robots" content="noindex">
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
function fibonacci (n, numArray) {
if (!numArray) {
var numArray = [1, 1];
}
numArray.push(numArray[numArray.length - 1] + numArray[numArray.length - 2]);
if (numArray.length < n) {
fibonacci (n, numArray);
} else {
console.log (numArray);
}
}
fibonacci (5);
fibonacci (3);
fibonacci (1);
fibonacci (10);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment