Skip to content

Instantly share code, notes, and snippets.

View tjeastmond's full-sized avatar
🥸
Grr data

TJ Eastmond tjeastmond

🥸
Grr data
View GitHub Profile
@tjeastmond
tjeastmond / when.js
Created March 21, 2012 20:24
Underscore.js Mixin that takes two functions as arguments, and returns a new function that when called will run the first function until it returns true. When the first function returns true, the second function is fired.
// When.js
// TJ Eastmond <tj.eastmond@gmail.com>, SpiteShow
// Simple Underscore.js Mixin that runs the first function until
// it returns true, then runs the second
(function() {
// Pass in two functions. The first is checked until it returns true, then the second is run
var when = function(truthy, func) {
// Just making sure we were passed functions...
@tjeastmond
tjeastmond / JSModule.js
Last active October 2, 2015 08:18
JS Module Pattern - Simple example for a friend
(function() {
var SpotifySearch = window.SpotifySearch = function() {
};
}).call(this);
var isInteger = function(x) { return (x ^ 0) === x; };
function isPrime(number) {
if (typeof number !== 'number' || !isInteger(number)) {
// Alternatively you can throw an error.
return false;
}
if (number < 2) {
return false;
for (var i = 1; i <= 100; ++i) {
var f = i % 3 === 0, b = i % 5 === 0;
console.log(f ? b ? 'FizzBuzz' : 'Fizz' : b ? 'Buzz' : i);
}
var memoizer = function(memo, func) {
var recur = function(n) {
var result = memo[n];
if (typeof result !== 'number') {
result = func(recur, n);
memo[n] = result;
}
return result;
};
return recur;
@tjeastmond
tjeastmond / innerScope.js
Created February 20, 2019 20:42
Helping someone with a JS question
function head() {
console.log("HEAD");
/* Some more logic that goes down here then will return function */
return function body(arg) {
console.log(arg);
function inner() {
console.log("INNER METHOD");
}
// return "SOMETHING USEFUL";
@tjeastmond
tjeastmond / getNames.js
Created March 6, 2019 21:12
Because I couldn't not do it...
function getNames(person) {
const names = [];
names.push(person.name);
person.children.forEach(child => names.push(...getNames(child)));
return names;
}
const people = {
name: "Robin",
children: [
/**
* Simple mergesort exercise
*/
const mergeSort = unsortedArray => {
if (unsortedArray.length <= 1) return unsortedArray;
const middle = Math.floor(unsortedArray.length / 2);
const left = unsortedArray.slice(0, middle);
const right = unsortedArray.slice(middle);
return merge(mergeSort(left), mergeSort(right));
// quicksort
const quicksort = (arr, left = 0, right = arr.length - 1) => {
if (left >= right) return;
const pivot = arr[Math.floor((left + right) / 2)];
const index = partition(arr, left, right, pivot);
quicksort(arr, left, index - 1);
quicksort(arr, index, right);
return arr;
};
// reverse character array
const reverse = arr => {
if (arr.length === 1) return arr;
let left = 0;
let right = arr.length - 1;
while (left < right) {
[arr[left], arr[right]] = [arr[right], arr[left]];
left++;
right--;