Skip to content

Instantly share code, notes, and snippets.

View sidd-at-git's full-sized avatar
♦️

Sidd @ Git sidd-at-git

♦️
View GitHub Profile
@sidd-at-git
sidd-at-git / boiler.js
Created September 19, 2015 12:12
JS BP
(function(){
'use strict';
}());
@sidd-at-git
sidd-at-git / gist:62d1ca570f5cb7f434b2
Created May 13, 2014 06:53
Javascript: Sorting Algos
// Bubble Sort
// The first sorting algorithm we will examine is the bubble sort.
// The bubble sort is one of the slowest sorting algorithms,
// but it is also one of the easiest sorts to implement.
// The bubble sort gets its name because when data are sorted using the algorithm,
// values float like a bubble from one end of the array to the other.
// Assuming you are sorting a set of numbers into ascending order,
// larger values float to the right of the array and lower values float to the left.
// This behavior is the result of the algorithm moving through the array many times,
// comparing adjacent values, and swapping them if the value to the left is greater than the value to the right.
@sidd-at-git
sidd-at-git / gist:192459db6b31bf9b0bbc
Created May 13, 2014 06:10
Javascript: Array Methods
// The next iterator function, every(), applies a Boolean function to an array
// and returns true if the function can return true for every element in
// the array. Here is an example:
function isEven(num) {
return num % 2 == 0;
}
var nums = [2,4,6,8,10];
var even = nums.every(isEven);
if (even) {
@sidd-at-git
sidd-at-git / gist:834c909b77f4bb91cc12
Created May 13, 2014 06:04
Javascript: Sort array of numbers fix
function compare(num1, num2) {
return num1 - num2;
}
var nums = [3,1,2,100,4,200];
nums.sort(compare);
console.log(nums); // 1,2,3,4,100,200
@sidd-at-git
sidd-at-git / gist:7511f78760702d0d8ce5
Created April 27, 2014 14:51
Javascript: Method Overloading in Alternate Form
// Add function for both number & string
function add(options) {
var args = arguments,
sum;
if(options.type == 'number') {
sum = 0;
for ( var i = 1; i < arguments.length; i++ ) {
sum += arguments[i];
}
} else if ( options.type == 'string' ) {
@sidd-at-git
sidd-at-git / gist:968481dbc716cf8a25e4
Created April 27, 2014 14:39
Javascript: Closures
/* Closure Examples
Compiled By: Siddharth Golchha
*/
// Use closure to call next.
var x = [1,2,3];
var setup = function(arr) {
var i = 0;
return function(){
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
@sidd-at-git
sidd-at-git / gist:d90837003c6508e5088f
Created April 20, 2014 04:31
Javascript: Few noteworthy code snippets
/* Few noteworthy code snippets
Compiled By: Siddharth Golchha
*/
// Get all the properties of Math object
// getOwnPropertyNames returns both enumerable and non enumerable
Object.getOwnPropertyNames(Math);
// Get all the properties of Array/String object
Object.getOwnPropertyNames( Array.prototype );
@sidd-at-git
sidd-at-git / gist:11093983
Created April 19, 2014 18:58
Javascript: Insertion Sort
/*
Insertion Sort in a traditional way
Practical Example: Sorting a pack of cards in hand from left to right.
Pick up 2nd card, compare with 1st card.
Pick up 3rd card, compare with 2nd card and 1st card.
And so on...
*/
function insertionSort(arr){
// arr = [10,43,-4,3,9,65,-35,8]
for (var i = 1; i < arr.length; i++) {
@sidd-at-git
sidd-at-git / gist:11082025
Created April 19, 2014 11:44
Javascript: random number
function randomNumber(min, max){
var min = min || 0,
max = max || 1;
return max>min ? ((Math.random())*(max-min)) + min : ((Math.random())*(min-max)) + max;
}