Skip to content

Instantly share code, notes, and snippets.

function share(users, amount){
// write your logic
}
const users = [
{user: 'a', ratio: '.45'},
{user: 'b', ratio: '.45'},
{user: 'c', ratio: '.10'}
]
const BLOOD_TYPES = {
"O−": ["O−", "O+", "A−", "A+", "B−", "B+", "AB−", "AB+"],
"O+": ["O+", "A+", "B+", "AB+"],
"A−": ["A−", "A+", "AB−", "AB+"],
"A+": ["A+", "AB+"],
"B−": ["B−", "B+", "AB−", "AB+"],
"B+": ["B+", "AB+"],
"AB−": ["AB−", "AB+"],
"AB+": ["AB+"]
@imsarvesh
imsarvesh / App.jsx
Last active March 29, 2020 20:09
useMemo Example
import { useState, useMemo } from 'react';
// Usage
function App() {
const [count, setCount] = useState(0);
const [wordIndex, setWordIndex] = useState(0);
const words = ['hey', 'this', 'is', 'cool'];
const word = words[wordIndex];
@imsarvesh
imsarvesh / gist:7aed0e21e28d36428e0a75558e4ec9b4
Created February 2, 2020 22:41
MLab Mongo Backup and Restore Locally
mongodump -h <subdomain>.mlab.com:25372 -u <username> -p <password> --authenticationDatabase <dbname> -d <backup-folder-name> -o ~/Desktop/
&&
mongorestore --db <dbname> ~/Desktop/<backup-folder-name>/
@imsarvesh
imsarvesh / oneliners.js
Created April 2, 2019 06:20 — forked from mikowl/oneliners.js
👑 Awesome one-liners you might find useful while coding.
// By @coderitual
// https://twitter.com/coderitual/status/1112297299307384833
// Remove any duplicates from an array of primitives.
const unique = [...new Set(arr)]
// Sleep in async functions. Use: await sleep(2000).
const sleep = (ms) => (new Promise(resolve => setTimeout(resolve, ms)));
// Type this in your code to break chrome debugger in that line.
function mergeSort(array) {
if(array.length < 2) return array;
var middle = Math.floor(array.length / 2);
var left = array.slice(0, middle);
var right = array.slice(middle);
return merge(mergeSort(left), mergeSort(right));
}
@imsarvesh
imsarvesh / PriorityQueue.js
Last active January 19, 2019 05:07
Priority Queue JavaScript Data Structure
//Queue Data Structure
function createQueue(){
const q = [];
return {
//enque
enque(item){
return q.push(item);
},
//deque
@imsarvesh
imsarvesh / queue.js
Created January 19, 2019 04:23
Queue Data Structure
//Queue Data Structure
function createQueue(){
const q = [];
return {
//enque
enque(item){
return q.push(item);
},
//deque
@imsarvesh
imsarvesh / The Observer Pattern
Created January 13, 2019 15:12
Javascript Design Pattern : The Observer Pattern
var eventManager = function(){
var subscriptions = {};
function subscribe(type, fn){
if(!subscriptions[type]) {
subscriptions[type] = [];
}
if(subscriptions[type].indexOf(fn) == -1){
subscriptions[type].push(fn);
var insertionSort = (arr) => {
arr = arr.slice();
for(var i = 1; i < arr.length; i++){
var current = arr[i];
var j = i - 1;
while(j >= 0 && arr[j] > current){
arr[j + 1] = arr[j];
j--;
}