Recursion Recursion Recursion
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Understanding
Recursion
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Overview
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Recursion
recursion is a process in which a function calls itself as a subroutine. this allows the function to be repeated several times, since it calls itself during its execution. Functions that incorporate recursion are called recursive functions.
Definition
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Recursion
Recursion is used for big problems that can be broken into multiple, smaller pieces.
Why do we use recursion?
Factorial: The product of an integer and all the integers below it.
var tenFactorial = 4 * 3 * 2 * 1;
console.log(tenFactorial) // 24
big problem
smaller pieces
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Recursion
Let's take a look at what this looks like in code.
Why do we use recursion?
var factorial = function(num){
if (num <= 1){
return 1;
} else {
return num * factorial(num-1);
}
}
big problem
smaller pieces
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Recursion
- Base Case: The condition that triggers the end of the current recursive branch. Goes at the top of your function. Code inside always returns, but does not have to return a value.
- Recursive Call: If the base-case does not trigger, we will invoke our recursive function passing in a new value. The new value will move must move us closer to our base case.
- Return statement: You must provide a return statement somewhere outside of your base case. If not, your program will run infinitely.
Recursion Rules
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Recursion
See them in action.
Recursion Rules
var factorial = function(num){
if (num <= 1){
return 1;
} else {
return num * factorial(num-1);
}
}
base case
action step/recursive call
return statements
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Recursion
Recursion and iteration are very similar and can sometimes be used interchangeably.
Recursion vs Iteration
var factorial = function(num){
if (num <= 1){
return 1;
} else {
return num * factorial(num-1);
}
}
var factorial = function(num){
var total = 1;
for (var i = num; i > 1; i--){
total *= i;
}
return total;
}
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Recursion
Iteration is best when you'd like to search through mostly linear data.
Linear Search
var linearArray = [1, 2, 3, 4, 5, 6, 7];
for (var i = 0; i <= linearArray.length; i++){
// looping straight through the array.
console.log(linearArray[i]);
};
11
12
13
14
15
16
17
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Recursion
Iteration is also good for simply nested data.
Nested Data Search
var nestedArray = [1, 2, [3, 4, 5] 6, 7];
for (var i = 0; i <= nestedArray.length; i++){
if (Array.isArray(nestedArray[i]){
for (var j = 0; j <= nestedArray[i].length; j++){
console.log(nestedArray[i][j];
}
} else {
console.log(nestedArray[i]);
}
};
11
12
13
14
15
1[ ]
17
16
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Recursion
var treeOfNumbers = [
[1, 2, [3, [4, [5]], 6]],
[7, [8, 9]],
[10, [11, [12]]],
];
A tree is a collection of data with many embedded data structures. The internal data structures are called branches.
Tree Data Structure
branches
tree structure
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Recursion
var countEvens = function(tree){
// base case?
// action step?
// return statement?
};
var treeOfNumbers = [
[1, 2, [3, [4, [5]], 6]],
[7, [8, 9]],
[10, [11, [12]]],
];
Recursion is the best method to use when it comes to tree structures.
Recursion!
big problem: looping through tree of arrays.
smaller problem: looping through one array.
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Recursion
var countEvens = function(tree){
// base case?
// action step?
// return statement?
};
Solve it yourself! Pause the video and give yourself ~15 minutes to solve this function.
Recursion!
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Recursion
Recursion is the best method to use when it comes to tree structures.
Recursion!
var countEvens = function(tree){
var count = 0;
for (var i = 0; i < tree.length; i++){
if (Array.isArray(tree[i])){
// action step/recursive call
count += countEvens(branch[i]);
} else {
if(tree[i] % 2 === 0){
//action step
count += 1;
}
}
}
// base case & return statement
return count;
};
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Recursion
Test your understanding!
// Refactor this contains function into a recursive function.
// contains takes a collection and a value and returns true
// if the value exists within the collection
// and false if it does not.
var contains = function(collection, inputValue){
var state = false;
collection.forEach(function(element){
if (element === inputValue){
state = true;
}
});
return state;
};
Recursion Lecture!
By telegraphprep
Recursion Lecture!
Understanding recursion, recursion, recursion, recursion, recursion, recursion, recursion, recursion, recursion.
- 877