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 generate = function(numRows) { | |
var result = []; | |
for(var i = 1; i <= numRows; i++) { | |
var inner = []; | |
for(var j = 0; j < i; j++) { | |
inner.push(Math.abs(j + 1)); | |
} | |
if (j%2 > 0) { | |
for(var q = i - 1; q >= 1; q--) { | |
inner.push(Math.abs(q)); |
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 generate = function(numRows) { | |
var result = []; | |
for(var i = 1; i <= numRows; i++) { | |
var inner = []; | |
for(var j = 0; j < i; j++) { | |
inner.push(Math.abs(j + 1)); | |
} | |
for(var q = i - 1; q >= 1; q--) { | |
inner.push(Math.abs(q)); |
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 HashTable = function(limit=8) { | |
this._limit = limit; | |
this._storage = []; | |
this._count = 0; | |
} | |
//this._storage = [/*container*/[/*bucket*/], [[/*tuple*/], []]] |
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
String.prototype.hashCode = function(max){ | |
var hash = 0; | |
if (!this.length) return hash; | |
for (i = 0; i < this.length; i++) { | |
char = this.charCodeAt(i); | |
hash = ((hash<<5)-hash)+char; | |
hash = hash & hash; // Convert to 32bit integer | |
} | |
return Math.abs(max?hash%max:hash); | |
}; |
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
HashTable.prototype.add = function(key, value) { | |
var index = key.hashCode(); | |
var tuple = [key, value]; | |
var bucket = this._storage.get(index); | |
if (bucket) { | |
for (var i = 0; i < bucket.length; i++) { | |
if (bucket[i][0] === key) { | |
bucket[i][1] = value; | |
} else { | |
bucket.push(tuple); |
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
HashTable.prototype.retrieve = function(key) { | |
var index = key.hashCode(); | |
var bucket = this._storage.get(index); | |
for (var i = 0; i < bucket.length; i++) { | |
if (bucket[i][0] === key) { | |
return bucket[i][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
class CashAmount { | |
constructor(amount) { | |
this.amount = amount; | |
} | |
} | |
CashAmount.prototype.totalInPennies = function() { | |
var pennies = this.amount * 100; | |
return pennies; | |
}; |
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 exponent = function(base, exp) { | |
if(exp === 0) { | |
return 1; | |
} | |
if(exp < 0) { | |
return 1/(base * exponent(base, -(exp+1))); | |
} | |
return base * exponent(base, exp-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
var arraySum = function(array) { | |
var total = 0; | |
if(array.length === 0) { | |
return 0; | |
} | |
var popped = array.pop(); | |
if(Array.isArray(popped)) { | |
total += arraySum(popped); | |
return total += arraySum(array); | |
} |
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 factorial = function(n) { | |
if(n < 0) {return null}; | |
var result = 1; | |
if(n > 0) { | |
result = result * n * factorial(n - 1); | |
} return result; | |
}; |
OlderNewer