This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// When.js | |
// TJ Eastmond <[email protected]>, 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... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var SpotifySearch = window.SpotifySearch = function() { | |
}; | |
}).call(this); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getNames(person) { | |
const names = []; | |
names.push(person.name); | |
person.children.forEach(child => names.push(...getNames(child))); | |
return names; | |
} | |
const people = { | |
name: "Robin", | |
children: [ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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--; |
OlderNewer