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.
Recursion is used for big problems that can be broken into multiple, smaller pieces.
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
Let's take a look at what this looks like in code.
var factorial = function(num){
if (num <= 1){
return 1;
} else {
return num * factorial(num-1);
}
}
big problem
smaller pieces
See them in action.
var factorial = function(num){
if (num <= 1){
return 1;
} else {
return num * factorial(num-1);
}
}
base case
action step/recursive call
return statements
Recursion and iteration are very similar and can sometimes be used interchangeably.
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;
}
Iteration is best when you'd like to search through mostly linear data.
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
Iteration is also good for simply nested data.
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
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.
branches
tree structure
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.
big problem: looping through tree of arrays.
smaller problem: looping through one array.
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 is the best method to use when it comes to tree structures.
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;
};
// 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;
};