Skip to content

Instantly share code, notes, and snippets.

View greggnakamura's full-sized avatar

Gregg Nakamura greggnakamura

View GitHub Profile
@greggnakamura
greggnakamura / function-definition.js
Last active October 11, 2015 10:07
Javascript: functions
// Define a function within itself
var myfunction = function fun(number) {
myfunction = function( number ) {
return ++number;
};
return number * 2;
};
alert(myfunction(10)); // 20
alert(myfunction(10)); // 11
@greggnakamura
greggnakamura / gist:99a2b176ca7b188d5ff6
Last active August 29, 2015 14:02
Javascript: Find the index of the smallest element in an array
function indexOfSmallest(a) {
var lowest = 0;
for (var i = 1; i < a.length; i++) {
if (a[i] < a[lowest]) lowest = i;
}
return lowest;
}
var a = [1, 2, 3, 4, 4, 6, 5, 0, 9];
@greggnakamura
greggnakamura / codeeval.js
Last active August 29, 2015 14:05
CodeEval: FizzBuzz (Solved)
// user input
var input = '3 5 10\n2 7 15\n5 10 50';
// split on newline
var inputSplitOnNewLine = input.split('\n');
// get and set 'A', 'B', and 'N' values
// call fizzBuzz function
for (var i = 0; i < inputSplitOnNewLine.length; i++) {
var inputString = inputSplitOnNewLine[i]; // current 3 numbers
@greggnakamura
greggnakamura / gist:fa3c88bbf1f311fb5cc2
Created September 9, 2014 04:52
Javascript: Self-invoking anonymous function example
// Immediately invoked function
// Code treated as local variables and functions
// Anonymous function, wrapped by a set of parenthesis, allows execution of anonymous function
(function () {
var foo = 'hello, window';
var bar = function () {
var foo = 'hello, function';
@greggnakamura
greggnakamura / gist:1e4ce72cbe5c5b402fb7
Last active August 29, 2015 14:07
Javascript: Namespacing with the Module Pattern
// http://javascriptweblog.wordpress.com/2010/12/07/namespacing-in-javascript/
/* Namespacing - The Module Pattern */
var myApp = (function () {
var id = 0;
return {
next: function () {
return id++;
},
@greggnakamura
greggnakamura / gist:f8eaf88411c7dd9e0b4a
Created January 3, 2015 22:14
Javascript: function example
/* functions */
/* passing a function as a parameter */
var calculate = function (number, paramTwo, fn) {
number = number + 3;
number = number + 1;
number = number * 8;
return fn(number, paramTwo);
};
@greggnakamura
greggnakamura / setInterval.js
Last active August 29, 2015 14:14
Javascript: setInterval example
(function () {
var speed = 500,
i = 0,
doSomething = function () {
console.log('doSomething() executed ' + (i + 1) + ' times');
i++;
if (i > 4) {
// stop when condition is met
@greggnakamura
greggnakamura / gist:d806336d8c0cebdc5f7e
Created February 1, 2015 20:02
Javascript: DOM level 0; Event Listeners (IE Legacy)
(function () {
// HTML items
// <button>Normal</button>
// <button>Changed</button>
// <a href="http://www.google.com">Google</a>
// <a href="http://www.google.com">Google</a>
var buttons = document.getElementByTagName('button');
@greggnakamura
greggnakamura / eventUtility.js
Created February 1, 2015 20:44
Javascript: Feature Detection, Cross-Browser Event Handling example
// eventUtility.js
var eventUtility = {
addEvent: function (el, type, fn) {
/*
el: HTML element object (DOM object)
type: event type to listen to
fn: function
*/
@greggnakamura
greggnakamura / jquery-child-event-delegation.js
Last active August 29, 2015 14:14
Javascript: Event delegation example
// event listener on each child node
$('#parent li').on('click', function(e) {
console.log('List item:', e.target.id, 'was clicked');
});