Skip to content

Instantly share code, notes, and snippets.

View mayashavin's full-sized avatar
:octocat:

Maya Shavin mayashavin

:octocat:
View GitHub Profile
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;
}
//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 handleScroll(){
}
var handleScrollListener = debounce(handleScroll, 200);
window.addEventListener('scroll', handleScrollListener);
//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;
@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 / 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)){
.parent{
position: relative;
display: inline;
}
.linkme{
position: absolute;
bottom: 0;
right: 0;
opacity: 0;
function Node(value) {
this.value = value;
this.next = undefined;
}
function SLinkedList() {
var head = undefined;
var length = 0;
return {
@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;