This file contains hidden or 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(){ | |
| 'use strict'; | |
| }()); |
This file contains hidden or 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
| // 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. |
This file contains hidden or 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
| // 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) { |
This file contains hidden or 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 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 |
This file contains hidden or 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
| // 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' ) { |
This file contains hidden or 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
| /* 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(){ |
This file contains hidden or 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 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); | |
| } |
This file contains hidden or 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
| /* 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 ); |
This file contains hidden or 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
| /* | |
| 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++) { |
This file contains hidden or 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 randomNumber(min, max){ | |
| var min = min || 0, | |
| max = max || 1; | |
| return max>min ? ((Math.random())*(max-min)) + min : ((Math.random())*(min-max)) + max; | |
| } |