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 runningSum(addends) { | |
var sums = [addends[0]]; | |
for (var i=1; i<addends.length; i++) { | |
sums.push(addends[i]); | |
} | |
return sums; | |
} |
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
/* | |
usage: | |
var csvString = toCSV({ | |
key1: { col1: 5, col2: 'hi' }, | |
key2: { col1: 7, col2: 'hello' } | |
}, { | |
// default config: | |
includeKeys: true, | |
delimiter: ',', |
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
// Zip arrays into one | |
// Example with two arrays: value 0 from a, value 0 from b, value 1 from a, etc. | |
function zipArrays() { | |
var zipped = []; | |
var arrays = [].slice.call(arguments); | |
for (var valueI = 0; arrays.length > 0; valueI++) { | |
for (var arrayI = 0; arrayI < arrays.length; arrayI++) { | |
if (arrays[arrayI].length - 1 < valueI) { | |
arrays.splice(arrayI, 1); | |
continue; |
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
/* differentiate nested <mark> elements with a gamut of colors */ | |
mark { | |
color: #000; | |
transition: background-color 0.2s ease-in; | |
} | |
mark, | |
mark:hover, | |
mark:hover mark { | |
background: hsl(0, 100%, 90%); |
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 getProperties(obj) { | |
var newObj = {}; | |
for (var key in obj) { | |
newObj[key] = true; | |
} | |
return newObj; | |
} | |
function haveSameProperties(a, b) { | |
return JSON.stringify(getProperties(a)) === JSON.stringify(getProperties(b)); |
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 getGaps(nums) { | |
var gaps = []; | |
for (var i = 1; i < nums.length; i++) { | |
gaps.push(nums[i] - nums[i-1]); | |
} | |
return gaps; | |
} |
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 getRunLengths(array) { | |
var runLengths = []; | |
var curValue; | |
for (var i = 0; i < array.length; i++) { | |
if (array[i] === curValue) { | |
runLengths[runLengths.length - 1]++; | |
} | |
else { | |
runLengths.push(1); | |
curValue = array[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 log = (function() { | |
function qs(selector, scope) { | |
return (scope || document).querySelector(selector); | |
} | |
var log = qs('#log'); | |
return function(msg) { | |
var p = document.createElement('p'); | |
p.style.whiteSpace = 'pre'; |
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 displayProps(obj, indent) { | |
indent = indent || '\t'; | |
var str = ({}).toString.call(obj).slice(8,-1) + ':\n'; | |
for (var key in obj) { | |
var val = typeof obj[key] === 'object' ? | |
displayProps(obj[key], indent + '\t') : obj[key]; | |
str += indent + key + ': ' + val + '\n'; | |
} | |
return str; | |
} |
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
// create 2d array or multidimensional matrix | |
function createMatrix() { | |
var matrix = []; | |
matrix.add = function(val, i1, i2/*, ...*/) { | |
var cur = matrix, curI; | |
// loop from 2nd argument to 2nd to last | |
for (var i = 1; i < arguments.length - 1; i++) { | |
curI = arguments[i]; | |
NewerOlder