Skip to content

Instantly share code, notes, and snippets.

View bradoyler's full-sized avatar
👋
say hi

brad oyler bradoyler

👋
say hi
View GitHub Profile
@bradoyler
bradoyler / debounce.js
Last active August 29, 2015 14:03
debounce javascript function
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this,
args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function() {
timeout = null;
if (!immediate) {
func.apply(context, args);
@bradoyler
bradoyler / winWidth.js
Last active November 12, 2017 10:29
cross browser window width.
// cross browser window width
var winWidth = function() {
var w = 0;
// IE
if (typeof( window.innerWidth ) != 'number') {
if (!(document.documentElement.clientWidth === 0)) {
@bradoyler
bradoyler / ackermann.js
Last active August 29, 2015 14:04
Ackermann function: In the 1920s, Wilhelm Ackermann demonstrated a computable function that was not primitive-recursive, settling an important argument in the run-up to the theory of computation. There are several versions of his function, of which the most common is defined over non-negative integers m and n. The function grows very rapidly, ev…
function ack(m,n) {
return (m == 0) ?
n + 1 :
(m > 0 && n==0) ?
ack(m-1,1) :
(m > 0 && n > 0) ?
ack(m - 1, ack(m,n-1)) :
undefined;
}
package main
import (
"fmt"
)
func main() {
fmt.Println("ack(3, 4):", ack(3, 4))
}
@bradoyler
bradoyler / findIds.js
Created August 2, 2014 15:38
filter array & display time it occured
function findIds(arr, filterFn) {
var results = arr.filter(filterFn);
results.timestamp = new Date();
return result;
}
var ids = [0, 1, 2, 8, 4, 4, 5, 6, 7, 8, 9];
var filtered = findIds(ids, function(x) {
return (x == 8);
@bradoyler
bradoyler / middleware.js
Created August 2, 2014 15:51
express middleware module template (NodeJs)
module.exports.doSomething = function() {
return function(req, res, next) {
console.log('do something...');
//next();
}
};
@bradoyler
bradoyler / snippets.js
Last active January 30, 2017 17:15
Data structures and algorithms using javascript. Computer science FTW.
var array = [0, 1, 2];
var weekDays = ['sun','mon','tues','wed','thurs','fri','sat'];
var str = "test";
var obj = {};
for (var i = 0; i < array.length; i++) {
var test = array[i];
}
@bradoyler
bradoyler / parentheses_check.js
Last active August 29, 2015 14:05
check for balanced parens
function parenthesesAreBalanced(s)
{
var parentheses = "[]{}()",
stack = [], //Parentheses stack
i, //Index in the string
c; //Character in the string
for (i = 0; c = s[i++];)
{
var bracePosition = parentheses.indexOf(c),
@bradoyler
bradoyler / binarySearch.js
Last active July 27, 2018 06:09
search an array with binary search algorithm - iterative & recursive approaches
// iterative approach
function binarySearch(arr, searchElement) {
var minIndex = 0;
var maxIndex = arr.length - 1;
var currentIndex;
var currentElement;
while (minIndex <= maxIndex) {
currentIndex = (minIndex + maxIndex) / 2 | 0;
@bradoyler
bradoyler / pdkplayer.html
Last active August 29, 2015 14:06
minimal PDK player example // source http://jsbin.com/miwaca/7
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="minimal PDK player example" />
<meta name="tp:EnableExternalController" content="true" />
<meta charset="utf-8">
<title>minimal PDK player</title>
</head>
<body>