Navigation Menu

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 / 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;
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);
//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);
},
function FlattenArray(arr){
var flattened = [];
while(arr.length){
var element = arr.pop(); //start from end
//if (element instanceof Array)
//if (Object.prototype.toString.call(element) === '[object Array]')
if (Array.isArray(element)){
arr = arr.concat(element);
//Given input:
// could be potentially more than 3 keys in the object above
// items = [
// {color: 'red', type: 'tv', age: 18},
// {color: 'silver', type: 'phone', age: 20}
// ...
// ]
// excludes = [
/*
If you were building a search tool and wanted search results to pop up as you
typed but the server call was taxing, write a function that gets called on every
key down but calls the server when the user stops typing for 400ms.
*/
// <input type="text" class="js-search">
/*
Questions to ask:
1. Each of input should have unique id, where is the id in this case?