Skip to content

Instantly share code, notes, and snippets.

View mayashavin's full-sized avatar
:octocat:

Maya Shavin mayashavin

:octocat:
View GitHub Profile
.parent{
position: relative;
display: inline;
}
.linkme{
position: absolute;
bottom: 0;
right: 0;
opacity: 0;
@mayashavin
mayashavin / Emitter_ES6.js
Last active May 22, 2018 16:52
Write Emitter Class
class Emitter{
constructor(){
this.events = new Map();
}
subscribe(event, callback){
if (!event || !callback || !(callback instanceof Function)) return;
let index = 0, events = this.events;
if (events.has(event)){
//Given an input array and another array that describes a new index for each element,
// mutate the input array so that each element ends up in their new index.
// Discuss the runtime of the algorithm and how you can be sure there won't be any infinite loops
// Input: [a,b,c,d,e,f,g] & [3,6,2,4,0,1,5]
// Output: [e,f,c,a,d,g,b]
// Question to ask: Input validation? Length same? indices array has to contains only numbers from 0 -> length - 1.
function reorderArr(arr, indices){
//Check if arrays are same length
if (arr.length !== indices.length || invalidOrder(indices, arr.length - 1)) return;
function handleScroll(){
}
var handleScrollListener = debounce(handleScroll, 200);
window.addEventListener('scroll', handleScrollListener);
//Debounce returns a function, based on input:
//1. trigger function for debouncing - func
//2. time to debounce (waiting time - only call after this amount of secs passed - wait
//3. immediate - true/false whether trigger function immediately or wait until the debouncing time.
//Logic:
//1. Save arguments, save context
//2. Clear timeout - start again the counter
//3. if (immediate and no timeout - aka first run - run it.
//4. set Time out for triggering func with wait time, in which if not immediate (false), then clear timeout and trigger func.
function debounce(func, wait, immediate = false){
//Debounce returns a function, based on input:
//1. trigger function for debouncing - func
//2. time to debounce (waiting time - only call after this amount of secs passed - wait
//3. immediate - true/false whether trigger function immediately or wait until the debouncing time.
//Logic:
//1. Save arguments, save context
//2. Clear timeout - start again the counter
//3. if (immediate and no timeout - aka first run - run it.
//4. set Time out for triggering func with wait time, in which if not immediate (false), then clear timeout and trigger func.
function debounce(func, wait, immediate = false){
function firstBadVersion(arr){
let found = undefined, start = 0, end = arr.length;
while (start < end && !found){
let mid = Math.floor((start + end)/2);
if (isBad(arr[mid])){
if (arr[mid - 1] && isBad(arr[mid - 1])){
end = mid - 1;
}
/*
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?
//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 = [
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);