This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* USING RECURSION | |
*/ | |
function factorialRecursion(n) { | |
if(n === 0 || n === 1) { | |
return 1; | |
} | |
return n * factorialRecursion(n - 1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Pair(a, b) { | |
this.firstElement = a; | |
this.secondElement = b; | |
} | |
function compareFirstElements(a, b) { | |
if(a.firstElement < b.firstElement) { | |
return -1; | |
} | |
if(a.firstElement > b.firstElement) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sampleAPrices = [100, 10, 50, 40, 20], | |
sampleADays = sampleAPrices.length, | |
//always making a loss | |
sampleBPrices = [30, 25, 12, 8, 1], | |
sampleBDays = sampleBPrices.length, | |
sampleCPrices = [7, 1, 5, 3, 6, 4], | |
sampleCDays = sampleCPrices.length; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var home_values = [10, 20, 30, 40, 50]; | |
//var home_values = [10,5,2,12]; | |
var home_count = home_values.length; | |
function rob_recursive(count, nums) { | |
//base case | |
if(count === 0) return 0; | |
if(count === 1) return nums[0]; | |
if(count === 2) return Math.max(nums[0], nums[1]); | |
var maxValue = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var rLength = 4, | |
rPrices = [1, 5, 8, 9]; | |
//RECURSIVE APPROACH | |
function TotalRevenue(rLen, prices) { | |
//define base case | |
if(rLen === 0) { | |
return 0; | |
} | |
//define a var to store the current max returned from recursion. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Using Recursive Programming | |
*/ | |
function climbStairs(n) { | |
return (n < 2) ? n : climbStairs(n - 1) + climbStairs(n - 2); | |
} | |
//var numOfWays = climbStairs(3); | |
//console.log(numOfWays); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var COUNT = 40; | |
/* Run Fibonnaci using the iterative approach*/ | |
function iterativeFibonacci(n) { | |
var firstPrevious = 0, | |
secondPrevious = 1, | |
nextSeqNumber; | |
//stop the first and second numbers | |
var results = [firstPrevious, secondPrevious]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var book = { | |
tarzan : { publisher: "Pub A", author: "Author A", pageCount: 300}, | |
"hidden universe" : { publisher: "Pub B", author: "Author B", pageCount: 1000}, | |
addBook : ...., | |
removeBook : function(name) { | |
this[name].name = name; //<- | |
var temp = this[name]; | |
delete this[name]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var book = { | |
tarzan : { publisher: "Pub A", author: "Author A", pageCount: 300}, | |
"hidden universe" : { publisher: "Pub B", author: "Author B", pageCount: 1000}, | |
addBook : ...., | |
removeBook : function(name) { | |
var temp = this[name]; | |
delete this[name]; | |
return temp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var book = { | |
tarzan : { publisher: "Pub A", author: "Author A", pageCount: 300}, | |
cinderella : { publisher: "Pub B", author: "Author B", pageCount: 400}, | |
"hidden universe" : { publisher: "Pub C", author: "Konstantin", pageCount: 1000}, | |
addBook : function(name, publisher, author, pageCount) { | |
this[name] = {publisher: publisher, author:author, pageCount}; | |
} | |
}; | |
book.addBook('JS Objects', "Publisher Z", "Konstantin", 300); |