Skip to content

Instantly share code, notes, and snippets.

@sfrapoport
sfrapoport / script.js
Last active August 27, 2015 15:19 — forked from ibrand/script.js
A canvas implentation of animating using a spritesheet
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d'),
timeBetweenSprites = 100, // measured in in milliseconds
direction = "forward";
canvas.width = 400;
canvas.height = 400;
// first figure out how big the spritesheet is (pixel dimensions)
var spritesheet = new Image();
@sfrapoport
sfrapoport / TicTacToe
Created March 19, 2015 15:48
TicTacToe for HackerSchool interview
//Tic Tac Toe game for the JavaScript console
//Constants
//Array of potential winning cell combinations
var winCells = [[1,2,3], [4,5,6], [7,8,9], [1,4,7], [2,5,8], [3,6,9], [1,5,9], [3,5,7]];
//Board model
function Board() {
@sfrapoport
sfrapoport / mergeSort
Created March 9, 2015 17:49
Merge Sort implementation
function mergeSort(arr) {
var len, arr1, arr2;
len = arr.length;
if (len===1) {
return arr;
}
//Split array in half
arr1 = arr.slice(0, Math.floor(len/2));
arr2 = arr.slice(Math.floor(len/2));
//Call mergeSort on each half
@sfrapoport
sfrapoport / findFirst
Created February 20, 2015 02:35
JavaScript function that takes 2 strings: str and set. The function will return the index of the first appearance of any char from set in str in O(m+n) time.
function findFirst(str, set) {
//Helper function to convert string into a hash object
function hashFromStr(str) {
var setHash = {},
key;
for (var i=0, len=str.length; i<len; i++) {
key = str[i];
if (setHash.hasOwnProperty(key)) {
setHash[key]++;
} else {