Skip to content

Instantly share code, notes, and snippets.

View cstephe's full-sized avatar

Chris Stephens cstephe

View GitHub Profile
@cstephe
cstephe / script.js
Created August 8, 2015 17:45
string compare
var testData = ['yeti', 'a dude', 'faceball 5000', 'zoolander', 'surfing', '', '90', '100']
var lexiconalCompare = function(a, b) {
if (typeof a === 'string' && typeof b === 'string') {
a = a.toLowerCase();
b = b.toLowerCase();
var bigLength = a.length > b.length ? a.length : b.length;
for (var x = 0; x < bigLength; x++) {
var aCharValue = a[x] ? a.charCodeAt(x) : 0,
bCharValue = b[x] ? b.charCodeAt(x) : 0
@cstephe
cstephe / backspacedirective
Last active March 9, 2021 02:15
Angular directive: prevent backspace from acting like the back button
.directive('backSpaceNotBackButton', [function(){
return {
restrict: 'A',
link: function(scope, element, attrs){
// This will stop backspace from acting like the back button
$(element).keydown(function (e) {
var elid = $(document.activeElement)
.filter(
"input:not([type], [readonly]),"+
"input[type=text]:not([readonly]), " +
@cstephe
cstephe / MergeSort
Last active December 25, 2015 00:39
Merge sort in JS: just keeping fresh with my CS stuff and doing it in JS for fun. So far thsi is memory optimized with merge only being created once just need to do it in-place and it should be even better.
var unsorted = [3, 6, 4, 5, 8, 1, 7, 9, 2];
var mergeSort = (function(){
var sort = function (toSort) {
var arlength = toSort.length;
var midpoint = Math.round(arlength / 2);
if (toSort.length > 1) {
// better to not slice here since it creates new arrays
return merge(mergeSort(toSort.slice(0, midpoint)),