Once Upon A Time...
Martin
The Dragon
&
Excuse me Mr. Dragon,
Are any of these numbers odd?
(3142 5798 6550 5914)
ANGRY DRAGON!
Sorry boy, I'll only tell you if the first number in that list is odd.
BUT I NEED TO KNOW IF ANY OF THE NUMBERS IN THE LIST ARE ODD!
SORRY BOY, I'LL ONLY TELL YOU IF THE FIRST NUMBER IN A LIST IS ODD
Hmmm.....
🤔
Ok fine, what about the first number in this list?
(3142 5798 6550 5914)
NOT ODD!
And what about the first number in this other list?
(5798 6550 5914)
NOT ODD!
Ok fine, what about the first number in this list?
(6550 5914)
NOT ODD!
Ok fine, what about the first number in this list?
(5914)
NOT ODD!
Ok fine, what about the first number in this list?
( )
That's an empty list you moron! There isn't a number in there!
(3142 5798 6550 5914)
(5798 6550 5914)
(6550 5914)
(5914)
()
No Odds
No Odds
No Odds
No Odds
No Odds
A process (a function in our case) that calls itself
Invoke the same function with a different input until you reach your base case!
The condition when the recursion ends.
This is the most important concept to understand
function countDown(num){
if(num <= 0) {
console.log("All done!");
return;
}
console.log(num);
num--;
countDown(num);
}
function sumRange(num){
if(num === 1) return 1;
return num + sumRange(num-1);
}
Can you spot the base case?
Do you notice the different input?
What would happen if we didn't return?
function sumRange(num){
if(num === 1) return 1;
return num + sumRange(num-1);
}
Let's break this down step by step!
function sumRange(num){
if(num === 1) return 1;
return num + sumRange(num-1);
}
sumRange(5)
sumRange(5)
sumRange(4)
sumRange(3)
sumRange(2)
sumRange(1)
function factorial(num){
if(num === 1) return 1;
return num * factorial(num-1);
}
Let's visualize the call stack!
function factorial(num){
if(num === 1) return 1;
return num * factorial(num);
}
function factorial(num){
if(num === 1) console.log(1) ;
return num * factorial(num-1);
}
function outer(input){
var outerScopedVariable = []
function helper(helperInput){
// modify the outerScopedVariable
helper(helperInput--)
}
helper(input)
return outerScopedVariable;
}
Let's try to collect all of the odd values in an array!
function collectOddValues(arr){
let result = []
function helper(helperInput){
if(helperInput.length === 0) {
return;
}
if(helperInput[0] % 2 !== 0){
result.push(helperInput[0])
}
helper(helperInput.slice(1))
}
helper(arr)
return result;
}
function collectOddValues(arr){
let newArr = [];
if(arr.length === 0) {
return newArr;
}
if(arr[0] % 2 !== 0){
newArr.push(arr[0]);
}
newArr = newArr.concat(collectOddValues(arr.slice(1)));
return newArr;
}
power(2,4) //16
power(3,2) //9
power(3,3) //27
Write a function which accepts a base an an exponent. It should return the result of raising the base to that exponent.
2 = 1
2 = 2 * 2
2 = 2 * 2
2 = 2 * 2
3
2
2
1
1
0
0
Write a function called productOfArray which takes in an array of numbers and returns the product of them all.
productOfArray([1,2,3]) // 6
productOfArray([1,2,3,10]) // 60
Write a function called productOfArray which takes in an array of numbers and returns the product of them all.
productOfArray([1,2,3]) // 6
productOfArray([1,2,3,10]) // 60
ES2015 allows for tail call optimization, where you can make some function calls without growing the call stack.
By using the return keyword in a specific fashion we can extract output from a function without keeping it on the call stack.
Unfortunately this has not been implemented across multiple browsers so it is not reasonable to implement in production code.