This file contains hidden or 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
// Since ES6 Map()'s' retain their order of insertion, it can sometimes be useful to get the last item or value inserted: | |
export const getLastItemInMap = map => Array.from(map)[map.size-1] | |
export const getLastKeyInMap = map => Array.from(map)[map.size-1][0] | |
export const getLastValueInMap = map => Array.from(map)[map.size-1][1] | |
// Obviously getLastKey and getLastValue can reuse getLastItem, but for maximum portability I opted for verbosity. |
This file contains hidden or 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 Mystack { | |
constructor() { | |
this.count = 0; | |
this.data = []; | |
} | |
push(item) { | |
this.data[this.count] = item; | |
this.count += 1; | |
return this.data; |
This file contains hidden or 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 Node { | |
constructor(value) { | |
this.value = value; | |
this.next = null; | |
} | |
} | |
class Queue { | |
constructor() { | |
this.first = null; |
This file contains hidden or 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
// new node structure | |
class Node { | |
constructor(value) { | |
this.value = value; | |
this.next = null; | |
} | |
} | |
// linkedlist implementation | |
class LinkedList { |
This file contains hidden or 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 HashTable { | |
constructor(size) { | |
this.data = new Array(size); | |
} | |
// hash method | |
hashKey(key) { | |
let hash = 0; | |
for (let i = 0; i < key.length; i++) { | |
hash = (hash + key.charCodeAt(i) * i) % this.data.length; |
This file contains hidden or 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 Myarray { | |
constructor(size) { | |
(this.data = {}), (this.length = 0); | |
for (let i = 0; i < size; i++) { | |
this.data[i] = undefined; | |
this.length++; | |
} | |
} |
This file contains hidden or 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 Node { | |
constructor(value) { | |
this.value = value; | |
this.right = null; | |
this.left = null; | |
} | |
} | |
class BinarySearchTree { | |
constructor() { |
This file contains hidden or 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
let flattenObj = function(inputObject) { | |
let flattenedObj = {}; | |
for (let obj in inputObject) { | |
if (inputObject[obj].constructor === Object) { | |
let flatObj = flattenObj(inputObject[obj]); | |
for(let i in flatObj) { | |
flattenedObj[i] = flatObj[i]; | |
} |
This file contains hidden or 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
Call(), apply() and Bind() Implementation: | |
// Bind implementation | |
Function.prototype.myBind = function(...args) { | |
let obj = this; | |
let params = args.slice(1); | |
return function(...args2) { | |
return obj.apply(args[0], [...params, ...args2]); | |
} |
This file contains hidden or 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
+++++++++++++++++ call() Implementation +++++++++++++++++++++ | |
Function.prototype.myCall = function(obj1){ | |
let random = Math.random(); | |
obj1[random] = this; | |
let args = []; | |
for( let i =1; i<arguments.length; i++ ){ | |
args.push(arguments[i]) |