This file contains 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
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; | |
} |
This file contains 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
//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){ |
This file contains 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
//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){ |
This file contains 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
function handleScroll(){ | |
} | |
var handleScrollListener = debounce(handleScroll, 200); | |
window.addEventListener('scroll', handleScrollListener); |
This file contains 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
//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; | |
This file contains 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
function Queue(){ | |
var storage = {}, | |
head = 0, | |
tail= 0; | |
return { | |
enQueue: function(item){ | |
storage[tail] = item; | |
tail++; | |
}, |
This file contains 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 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)){ |
This file contains 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
.parent{ | |
position: relative; | |
display: inline; | |
} | |
.linkme{ | |
position: absolute; | |
bottom: 0; | |
right: 0; | |
opacity: 0; |
This file contains 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
function Node(value) { | |
this.value = value; | |
this.next = undefined; | |
} | |
function SLinkedList() { | |
var head = undefined; | |
var length = 0; | |
return { |
This file contains 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
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; | |
OlderNewer