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
| var sum = 0; | |
| for (var i = 0; i < 1000; i++) { | |
| if(i % 3 === 0 || i % 5 === 0) { | |
| sum += i; | |
| } | |
| } | |
| console.log(sum); |
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
| // Class generator | |
| var Class = function(parentCls) { | |
| var Klass = function() { | |
| this.init.apply(this, arguments); | |
| }; | |
| if(parentCls){ | |
| var subClass = function(){}; | |
| subClass.prototype = parentCls.prototype; | |
| Klass.prototype = new subClass; |
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 | |
| (function() { | |
| function funcA() {} | |
| var funcB = function() {}; | |
| funcC = function() {}; | |
| })() | |
| console.log(typeof funcA); // undefined | |
| console.log(typeof funcB); // undefined | |
| console.log(typeof funcC); // 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 quickSort (arr) { | |
| if(arr.length <= 1) { | |
| return arr; | |
| } | |
| var pivot = choosePivot(arr); | |
| var divider = divide(arr, pivot); | |
| var a1 = quickSort(arr.slice(0, divider)); | |
| var a2 = quickSort(arr.slice(divider + 1, arr.length)); | |
| a1.push(arr[divider]); |
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 mergeSort (arr, st, end) { | |
| var res; | |
| st = st || 0; | |
| end = end || arr.length; | |
| if(end - st > 1) { | |
| var mid = st + Math.round((end - st)/2); | |
| var a1 = mergeSort(arr, st, mid); | |
| var a2 = mergeSort(arr, mid, end); | |
| res = merge(a1, a2); |
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 bubbleSort (arr) { | |
| var l = arr.length, | |
| tmp = null, | |
| j = l - 1, | |
| i; | |
| if(l < 2) return arr; | |
| for(; j >= 0; j--) { | |
| for(i = 0; i < j; 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 findAllCombinations(numParens) { | |
| var totalCombs = 0; | |
| if(numParens > 0) { | |
| totalCombs = attachParen('', numParens, numParens, totalCombs); | |
| } | |
| return totalCombs; | |
| } | |
| function attachParen(str, open, close, total) { | |
| if(open > 0) { |
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
| // Catalan Numbers to mathematically verify number of combinations | |
| // E.g, the number of possible combinations of parentheses | |
| // http://en.wikipedia.org/wiki/Catalan_number | |
| function catalanNumber(numParens) { | |
| var total = 1; | |
| for(var i = 2; i <= numParens; i++) { | |
| total *= (numParens + i) / 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
| // LinkedListNode Class (Module pattern with cached functions) | |
| !function(){ | |
| LinkedListNode = function LinkedListNode(data) { | |
| this.node = [data, null]; | |
| } | |
| LinkedListNode.prototype.getNext = getNext; | |
| LinkedListNode.prototype.getData = getData; | |
| LinkedListNode.prototype.setNext = setNext; | |
| LinkedListNode.prototype.setData = setData; |
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 balancedPars(str) { | |
| var result = 0; | |
| if(typeof(str) !== 'string') return false; | |
| if(str) { | |
| result = str.split('') | |
| .map(function(elem){ | |
| return elem === '(' ? 1 : (elem === ')' ? -1 : 0); | |
| }) |