Skip to content

Instantly share code, notes, and snippets.

@hamannjames
hamannjames / faceClock.js
Last active September 9, 2017 21:59
WIP. Clock plugin that will be extendable and is very performant. Right now only has analogue clock.
// Link to codepen https://codepen.io/jaspercreel/pen/MvxpKZ?editors=0010
const clock = function() {
const create = {
init (el, opts = {}) {
if (!(el instanceof HTMLElement)) {
console.log(el);
@hamannjames
hamannjames / clock.js
Last active August 16, 2017 16:27
Simple clock logic for an analogue clock on your screen.
// Link to code pen https://codepen.io/jaspercreel/pen/QMOepX
const secHand = document.getElementById('secHand');
const minHand = document.getElementById('minHand');
const hourHand = document.getElementById('hourHand');
const start = -90;
@hamannjames
hamannjames / map.js
Last active August 17, 2017 03:53
Mapping land
/*
So let's say we're writing a program that will find the number of islands given a picture from a satellite.
The picture has already been pre-processed and encoded to a 2D matrix where 1 means land. And 0 means water.
How many islands are there?
*/
// It is safe to assume the input will be an array of arrays and that the values are always 0, 1
// It is worth noting that every 1 does not mean an individual island. 1's can connect to form a single landmass
@hamannjames
hamannjames / trueClone.js
Last active August 9, 2017 00:53
This little function deep clones an array, instead of slice which preserves references to objects and nested arrays.
// Parts of this function were borrowed from http://blog.andrewray.me/how-to-clone-a-nested-array-in-javascript/
// Link to code pen: https://codepen.io/jaspercreel/pen/jLmEWL
function trueClone(item) {
let copy, i;
if (Array.isArray(item)) {
copy = item.slice(0);
for (i = 0; i < copy.length; i++) {
copy[i] = trueClone(copy[i]);
@hamannjames
hamannjames / largestOfDepth.js
Created August 4, 2017 03:50
Get largest value in tree depth using depth first search
function largestValuesInTreeRows(t) {
function depthSearch(current) {
let values = [];
let depth = 0;
function traverse(current, depth) {
if (!current) {
return;
}
function kthLargestInBST(t, k) {
var check = function (value) {
k--;
if (k === 0) {return value;}
else {return false;}
}
var loop = function (node) {
if (!node) {return false}
return loop(node.left) || check(node.value) || loop(node.right)
@hamannjames
hamannjames / SorterSearcher.js
Created July 31, 2017 06:52
This is a helper object I am creating that will allow for alternative sorting and searching solutions to the native JS implementation. It sets a standard, and optimizes certain actions, as well as addressing some strange native JS behavior (like treating integers as strings)
const Sorter = function (arr = [], key = null, def = null) {
state = {
arr,
key,
def
}
const proto = {
type: 'Sorter',
@hamannjames
hamannjames / binSearch.js
Created July 27, 2017 18:31
Binary search of sorted array values as numbers
// It is expected that the array will be sorted, otherwise you will not get a proper search
// code pen url: https://codepen.io/jaspercreel/pen/yoYYVN
const binSearch = (arr, key) => {
if (!Array.isArray(arr)) {
console.log('This binary search requires an array');
return;
}
if (!key) {
@hamannjames
hamannjames / _mixins.scss
Created July 26, 2017 18:45
My favorite mixins
$breakpoints: (
"phone": 400px,
"phone-wide": 480px,
"phablet": 560px,
"tablet-small": 640px,
"tablet": 768px,
"tablet-wide": 991px,
"desktop": 1200px,
"desktop-wide": 1440px
);
@hamannjames
hamannjames / possibilities.js
Last active July 26, 2017 22:16
Given a set of possibilities, determine the maximum reward in the possibilities given a certain constraint.
/*
For this particular example the constraint is you cannot take two possibilities
that are next to each other. A real world example might be that you cannot invest multiple months
in a row and want to have the best chance at the best ROI. Kind of dumb but the point is
if there are constraints you want to track the best possible values for every sequence of moves,
only keeping track of the best possible outcomes
*/
const bestOutcome = (arr) => {