Skip to content

Instantly share code, notes, and snippets.

Connor McSheffrey mcsheffrey

Block or report user

Report or block mcsheffrey

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:7608229
/**
* Product preview image in campaign header
* @type {Backbone View}
*/
TS.Campaign.MainProductView = Backbone.View.extend({
@mcsheffrey
mcsheffrey / Trigger <select> element via JavaScript
Created Oct 17, 2013
Useful for implementing custom dropdowns with native open states. Tested on iOS6/7. iPhone/iPad and stock Android browser running 4.0.3
View Trigger <select> element via JavaScript
<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() {
View Memoized Fibonacci Sequence
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 Sep 26, 2017
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…
View gist:6977999
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)
View Find first non-repeating character in a word
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 Sep 26, 2013
A Memoization Pattern - JavaScript Patterns
View gist:6719018
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 Sep 26, 2013
Init-Time Branching - JavaScript Patterns
View gist:6718840
// 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 Sep 14, 2013
Function that returns array of top 3 values of an object
View gist:6564211
var items = {
'a': 5,
'b': 10,
'c': 4,
'd': 11
},
tempArr = [],
returnArr = [];
function sortObject(items) {
@mcsheffrey
mcsheffrey / Permute
Created Sep 13, 2013
Permute array and return array of permutations
View Permute
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 Sep 1, 2013
Example Module pattern
View gist:6407659
carousel = function(){
var config = {
CSS:{
classes:{
current:'current',
scrollContainer:'scroll'
},
IDs:{
maincontainer:'carousel'
}
You can’t perform that action at this time.