Skip to content

Instantly share code, notes, and snippets.

@robertcdawson
Created July 21, 2017 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertcdawson/37790f108ff50117c8e20ba165623121 to your computer and use it in GitHub Desktop.
Save robertcdawson/37790f108ff50117c8e20ba165623121 to your computer and use it in GitHub Desktop.
Recursion 101 Recursive function example // source https://jsbin.com/putuluv
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Recursive function example">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Recursion 101</title>
</head>
<body>
<script id="jsbin-javascript">
// Recursion 101
var callCount = 0;
var recursion101 = function(num) {
console.log("recursion101(3) call count: " + ++callCount);
var sum = 0;
if (num <= 0) return num;
sum = num + recursion101(num - 1);
return sum;
};
recursion101(3);
</script>
<script id="jsbin-source-javascript" type="text/javascript">// Recursion 101
var callCount = 0;
var recursion101 = function(num) {
console.log("recursion101(3) call count: " + ++callCount);
var sum = 0;
if (num <= 0) return num;
sum = num + recursion101(num - 1);
return sum;
};
recursion101(3);</script></body>
</html>
// Recursion 101
var callCount = 0;
var recursion101 = function(num) {
console.log("recursion101(3) call count: " + ++callCount);
var sum = 0;
if (num <= 0) return num;
sum = num + recursion101(num - 1);
return sum;
};
recursion101(3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment