[CREDITS HERE]
Recursion & Pseudoclasses
Stacks & Queues
function callMyself() {
callMyself();
}
What if you do a Google search for recursion?
function callMyself() {
if() {
// base case
return;
} else {
// recursive case
callMyself();
}
return;
}
Advice: Use a bottom-up approach by looking for patterns by starting with small or first few cases
function computeFactorial(num) {
var result = 1;
for(var i = 2; i <= num; i++) {
result *= i;
}
return result;
}
results *= 2;
results *= 3;
results *= 4;
results *= 5;
If we call computeFactorial(5), then the loop will run:
function computeFactorial(num) {
if(num === 1) {
return 1;
} else {
return num * computeFactorial(num - 1);
}
}
5 * computeFactorial(4)
4 * computeFactorial(3)
3 * computeFactorial(2)
2 * computeFactorial(1)
1
If we call computeFactorial(5), then the recursion will run:
function logNumbers(start, end) {
for(var i = start; i <= end; i++) {
console.log(i);
}
}
function logNumbersRecursively(start, end) {
function recurse(i) {
console.log(i);
if(i < end) {
recurse(i + 1);
}
}
recurse(start);
}
function MyClass(input) {
this.myProp = input;
}
MyClass.prototype.myMethod = function() {
console.log('Check it out: ' + this.myProp);
};
Constructor
Properties
(per instance)
Methods (for all instances)
function MyClass(input) {
}
var myInstance = new MyClass('hello');
var myOtherInstance = new MyClass('bye');
myInstance.myMethod();
myOtherInstance.myMethod();
The results of the shared method depend on the unique instance properties
The last item added into the stack will be the first one taken out of the stack.
(aka LIFO)
(aka pushed onto)
(aka popped off)
[ TODO: Find diagram that is legal for commercial use ]
The first item added into the queue will be the first one taken out of the queue.
(aka FIFO)
(aka enqueued)
(aka dequeued)