Skip to content

Instantly share code, notes, and snippets.

View mcsheffrey's full-sized avatar

Connor McSheffrey mcsheffrey

View GitHub Profile
/**
* Product preview image in campaign header
* @type {Backbone View}
*/
TS.Campaign.MainProductView = Backbone.View.extend({
@mcsheffrey
mcsheffrey / Trigger <select> element via JavaScript
Created October 17, 2013 06:41
Useful for implementing custom dropdowns with native open states. Tested on iOS6/7. iPhone/iPad and stock Android browser running 4.0.3
<select id="option-style-select" name="option_style">
<option value="Tee (American Apparel)" selected>Tee (American Apparel)</option>
<option value="Canvas Ringspun Tee">Canvas Ringspun Tee</option>
<option value="Bella Ladies Relaxed Fit Tee">Bella Ladies Relaxed Fit Tee</option>
</select>
<button>Open Select</button>
<script>
$("button").click(function() {
var fibo_useless = function(g, n) {
return n == 0 ? 0 :
n == 1 ? 1 :
g(n-1) + g(n-2);
};
var cachify = function(f, cache) {
return function(n) {
if(!cache[n]) {
var g = cachify(f, cache);
@mcsheffrey
mcsheffrey / gist:6977999
Last active September 26, 2017 17:33
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. Run time of this solution is THETA(n) as indexOf is a constant-time operation since an array in…
var input = [1,2,3,4,5],
specArr = [0,2,1,4,3];
function mutate(input, specArr) {
var visited = [0,2]
for(var i=0; i<specArr.length; i++) {
var tmp;
//keep track of array items we've already looped through (wouldn't want to mutate twice :D)
function findObjPropInArr(arr, prop, val) {
for(var i = 0; i<arr.length; i++) {
if(arr[i][prop] === val) {
return i;
}
}
return -1;
}
function findFirstNoRepeat(word) {
@mcsheffrey
mcsheffrey / gist:6719018
Created September 26, 2013 19:05
A Memoization Pattern - JavaScript Patterns
var myFunc = function (param) {
if (!myFunc.cache[param]) {
var result = {};
// ... expensive operation ...
myFunc.cache[param] = result;
}
return myFunc.cache[param];
@mcsheffrey
mcsheffrey / gist:6718840
Created September 26, 2013 18:52
Init-Time Branching - JavaScript Patterns
// the interface
var utils = {
addListener: null,
removeListener: null
};
// the implementation
if (typeof window.addEventListener === 'function') {
utils.addListener = function (el, type, fn) {
el.addEventListener(type, fn, false);
@mcsheffrey
mcsheffrey / gist:6564211
Created September 14, 2013 18:09
Function that returns array of top 3 values of an object
var items = {
'a': 5,
'b': 10,
'c': 4,
'd': 11
},
tempArr = [],
returnArr = [];
function sortObject(items) {
@mcsheffrey
mcsheffrey / Permute
Created September 13, 2013 16:23
Permute array and return array of permutations
var temp = [];
var answer = [];
return doIt(arr);
function doIt(a) {
var i, len, item;
for (i = 0, len = arr.length; i < len; i++) {
// remove the item at index i
@mcsheffrey
mcsheffrey / gist:6407659
Created September 1, 2013 22:13
Example Module pattern
carousel = function(){
var config = {
CSS:{
classes:{
current:'current',
scrollContainer:'scroll'
},
IDs:{
maincontainer:'carousel'
}