Skip to content

Instantly share code, notes, and snippets.

View scriptonian's full-sized avatar

I-am Konstantin scriptonian

View GitHub Profile
@scriptonian
scriptonian / factorial.js
Created March 14, 2018 02:50
Factorial using Dynamic Programming
/*
* USING RECURSION
*/
function factorialRecursion(n) {
if(n === 0 || n === 1) {
return 1;
}
return n * factorialRecursion(n - 1);
}
@scriptonian
scriptonian / maxlengthchainpair.js
Created March 11, 2018 03:22
Maximum length chain pair
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) {
@scriptonian
scriptonian / buy-sell-stock.js
Created March 8, 2018 03:03
Javascript - Buy sell stock with dynamic programming
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;
@scriptonian
scriptonian / house-robber.js
Last active February 12, 2018 01:53
Javascript House Robber
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;
@scriptonian
scriptonian / cutting-rods.js
Last active February 4, 2018 18:49
Cutting Rods
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.
@scriptonian
scriptonian / climbing-stairs.js
Created February 3, 2018 22:33
Climbing Stairs - Dynamic Programming
/*
Using Recursive Programming
*/
function climbStairs(n) {
return (n < 2) ? n : climbStairs(n - 1) + climbStairs(n - 2);
}
//var numOfWays = climbStairs(3);
//console.log(numOfWays);
@scriptonian
scriptonian / fibonacci.js
Last active September 27, 2019 01:01
Fibonacci
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];
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];
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;
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);