Skip to content

Instantly share code, notes, and snippets.

View mayashavin's full-sized avatar
:octocat:

Maya Shavin mayashavin

:octocat:
View GitHub Profile
@mayashavin
mayashavin / Queue.js
Last active March 5, 2018 08:55
Data Structures - Queue Implementation in JS
function Queue(){
var storage = {},
head = 0,
tail= 0;
return {
enQueue: function(item){
storage[tail] = item;
tail++;
},
@mayashavin
mayashavin / Stack.js
Last active January 10, 2018 13:54
Data Structures - Stack in JS without using Array - Full Demo: https://jsfiddle.net/dpnminh/g1xy22cz/
function Stack(){
var stack = {};
var stackSize = 0;
return {
push: function(item){
stack[stackSize] = item;
stackSize++;
},
pop: function(){
@mayashavin
mayashavin / DeepEqual.js
Last active January 14, 2018 09:56
JS Exercises
function deepEqual(a, b){
if (typeof a === 'object' && a !== null && typeof b === 'object' && b !== null){
var isEqual = Object.keys(a).length === Object.keys(b).length;
if (isEqual){
for (var key in a){
isEqual &= b.hasOwnProperty(key);
if (isEqual){
isEqual &= deepEqual(a[key], b[key]);
//Input: Array of Arrays of Cells
var grid = [['name', 'height', 'country'],
['Kilimanjaro',5895, 'Tanzania'],
['Everest',8848,'Nepal'],
['Mount Fuji',3776,'Japan'],
['Mont Blanc',4808,'Italy/France'],
['Vaalserberg',323,'Netherlands'],
['Denali', 6168,'United States'],
['Popocatepetl', 5465,'Mexico']];
function Node(value) {
this.value = value;
this.next = undefined;
}
function SLinkedList() {
var head = undefined;
var length = 0;
return {
function Node(value) {
this.value = value;
this.next = undefined;
this.prev = undefined;
}
function DLinkedList() {
var head = undefined;
var tail = undefined;
var length = 0;
function isPermutation(strA, strB){
var isValid = true;
//First validity check
if (!strA || !strB || strA.length !== strB.length){
return !isValid;
}
//Get the map of characters and their occurences in strA
var charsMap = getMap(strA);
function isPermutation(strA, strB){
var isValid = true;
//First validity check
if (!strA || !strB || strA.length !== strB.length){
return !isValid;
}
var sortedA = sort(strA), sortedB = sort(strB);
@mayashavin
mayashavin / Emitter.js
Last active August 11, 2022 16:35
Emitter Class Implementation
function Emitter(){
var events = new Map();
return {
subscribe: function(event_name, callback){
//If events has event_name, then get the event Subscription and push to it.
//Else create new event, subscription and add to events.
var isEventExisted = events.has(event_name), index = 0;
//Given 2 identical DOM trees (but not equal) and one element of the first DOM tree,
//how would you find this element in the second DOM tree
//Questions to ask:
//1. Input: input node is inside DOM tree? Or need to check if it exists?
//2. Output node needs to be identical? Same tag?
//3. Are comments, texts considered as child nodes? - Yes, need to use childNodes, No, use children.
var sameLevelNodeIdenticalTrees = {
getIndexOf: function(nodeList, node){
return Array.prototype.indexOf.call(nodeList, node);
},