Skip to content

Instantly share code, notes, and snippets.

@jasonwaters
jasonwaters / covenant.js
Created February 16, 2017 21:30
A promise implementation
const noop = () => void 0;
function Covenant(fn) {
this.resolver = fn;
this.thenCallback = noop;
this.catchCallback = noop;
this.resolver((...args) => {
this.thenCallback(...args);
}, () => {
@jasonwaters
jasonwaters / jayquery.js
Created February 14, 2017 22:27
jayquery chaining
function jayquery(selector) {
const elements = document.querySelectorAll(selector);
return {
bgcolor: function(value) {
elements.forEach(element => element.style.background = value);
return this;
},
text: function(value) {
elements.forEach(element => element.innerText = value);
@jasonwaters
jasonwaters / simple-di.js
Created February 14, 2017 22:27
simple dependency injection
function randy() {
return Math.random();
}
function smoke() {
return "poof";
}
function depend(dependencies, fn) {
fn.apply(null, dependencies.map(dep => window[dep]));
@jasonwaters
jasonwaters / string-permutations.js
Last active March 10, 2017 18:55
String Permutations
function permute(arr, permutation=[], permutations=[]) {
if(arr.length === 0) {
permutations.push(permutation);
}else {
arr.forEach(item => {
permute(arr.filter(value => value != item), permutation.concat(item), permutations);
});
}
return permutations;
@jasonwaters
jasonwaters / davis-staircase.js
Created February 9, 2017 18:28
Recursion: Davis' Staircase
//https://www.hackerrank.com/challenges/ctci-recursive-staircase
function stairStepper(stepSizes) {
let answers = [1,1];
return function(numSteps) {
if(numSteps < 0) {
return 0;
}else if(typeof answers[numSteps] === 'undefined') {
answers[numSteps] = stepSizes.reduce((totalSteps, stepSize) => totalSteps + stairCombinations(numSteps-stepSize), 0);
}
@jasonwaters
jasonwaters / is-prime.js
Created February 9, 2017 17:33
is prime?
// A prime number is a natural number that has exactly two distinct natural number divisors: 1 and itself.
const isPrime = (function () {
let primes = {};
let notPrimes = {};
return function (number) {
if(primes[number]) return true;
if(number === 1 || notPrimes[number]) return false;
@jasonwaters
jasonwaters / merge-sort.js
Created February 7, 2017 22:46
Merge Sort
function merge(arrA, arrB) {
let idxA=0, idxB=0, result=[];
while(idxA < arrA.length || idxB < arrB.length) {
if(idxB === arrB.length || arrA[idxA] <= arrB[idxB]) {
result.push(arrA[idxA]);
idxA++;
}else if(idxA === arrA.length || arrB[idxB] <= arrA[idxA]) {
result.push(arrB[idxB]);
@jasonwaters
jasonwaters / running-median.js
Created February 6, 2017 22:56
Heaps: Find the Running Median
// https://www.hackerrank.com/challenges/ctci-find-the-running-median
let input = `6
12
4
5
3
8
7`;
@jasonwaters
jasonwaters / tries.js
Last active February 6, 2017 20:45
Tries: Contacts
// https://www.hackerrank.com/challenges/ctci-contacts
function Trie() {
this.data = {};
}
Trie.prototype.process = function(op, str) {
if(op === 'add') {
let data = this.data;
@jasonwaters
jasonwaters / ice-cream-purchase.js
Created February 2, 2017 21:00
Ice Cream Purchases
// https://www.hackerrank.com/challenges/ctci-ice-cream-parlor
// find 2 distinct flavors that would take up all of the money m
// for each trip print the id numbers of the two types of ice cream
// some ice creams may have the same price
function purchase(indexed, product) {
let result = Object.assign({}, indexed);