Skip to content

Instantly share code, notes, and snippets.

var ceil = 2;
function binRand() {
return Math.round(Math.random() * ceil);
}
function count(arr) {
var result = {};
for(var i=0;i<arr.length;i++) {
var current = arr[i];
@shockey
shockey / gist:73b3c6632cca5d718e62
Last active August 29, 2015 14:17
bubble sort
// @kyleshockey and @eoutterson
//var unsorted = [7,3,5,6,1,2,4];
//var sorted = [1,2,3,4,5,6,7];
function genRand(len, max) {
var returnable = [], max = max || len;
for(var i=0;i<len;i++){
@shockey
shockey / transform-obj.js
Last active August 29, 2015 14:17
transforms an object into a nested array
// transforms an object into a nested array
// passing "true" as the second argument attempts to use
// recursion to handle nested objects. otherwise,
// transform will not touch the nested object.
obj = {
a:"b",
c:"d",
e:"f",
g:{
@shockey
shockey / gist:05e009968c89ba8ceeb9
Created May 15, 2015 02:11
Heroku Environment Variable Command Generator
function parse(obj){
var str="heroku config:set --app YOURAPP";
for(var key in obj) {
str += (" " + key + "=" + "\"" + obj[key] + "\"");
}
return str;
};
@shockey
shockey / rot13.js
Created June 19, 2015 16:57
An alphabetical rot13/rot-n function
function rot13(str, n) {
n = typeof n === 'number' ? n : 13;
var ret = [];
for(var i=0;i<str.length;i++) {
var code = str.charCodeAt(i);
var newCode = genCode(code,code > 90 ? 97 : 63,n);
newCode += newCode < code ? 1 : 0;
ret.push(String.fromCharCode(newCode));
}
function genCode(code,offset,n) {
@shockey
shockey / rounding.js
Created July 29, 2015 19:50
JS rounding function, behaves like Math.round
var round = function (num) {
var decimalPart = num % 1,
intPart = num - ( num % 1 );
return decimalPart >= .5 ? intPart + 1 : intPart;
}
// test equivalence of Math.round with commonly used numbers
var isEquivalent;
for(var i = 0; i < 1000000; i += Math.random()) {
var has = function(arr,target) {
return arr.indexOf(target) > -1;
}
var balancedParens = function (str) {
var pairs = {
'{' : '}',
'[' : ']',
'(' : ')'
},
@shockey
shockey / minmax.js
Last active August 29, 2015 14:26
linear-time min/max helper functions for arrays of numbers
// time: O(n)
// space: O(1)
var max = function(arr) {
var returnable = Number.MIN_VALUE;
arr.forEach(function(v){
if(returnable < v) {
returnable = v;
}
})
return returnable;
@shockey
shockey / hn_seach.js
Last active August 29, 2015 14:26 — forked from kristopolous/hn_seach.js
hn job query search
function query() {
var
total = 0, shown = 0,
job_list = Array.prototype.slice.call(document.querySelectorAll('.c00,.cdd')),
query_list = Array.prototype.slice.call(arguments);
// turn them all off
job_list.forEach(function(node) {
node.parentNode.parentNode.parentNode.style.display = 'none';
total ++;
@shockey
shockey / matrixAdd.js
Last active August 29, 2015 14:28
two-dimensional matrix addition using map
// matrixAdd only works on balanced matrices.
var a =
[[1,5],
[2,9]]
var b =
[[5,6],
[3,4]]