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
| /* | |
| Find all unique triplets in the array which gives the sum of 0. | |
| a + b + c = 0; | |
| [-1, 0, 1, 2, -1, -4] ==> [[-1, 0, 1], [-1, -1, 2]]; | |
| */ | |
| var temp = [-1, 0, 1, 2, -1, -4]; |
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 createMapping = function(numbers) { | |
| var mapping = {}, | |
| size = numbers.length; | |
| for (var i = 0; i < size; i++) { | |
| mapping[numbers[i]] = i; | |
| } | |
| return mapping; |
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
| // Given an unsorted integer array, find the first missing positive integer | |
| // For example, | |
| // Given [1, 2, 0], return 3. | |
| // Given [3, 4, -1, 1], return 2. | |
| var firstMissingPositive = function(A){ | |
| var i = 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
| //Reverse digits of an integer | |
| //Given x = 123, return 321 | |
| //Given x = -123, return -321 | |
| var reverse = function(x) { | |
| var lastDigit = 0, | |
| result = 0, | |
| isNeg = true; | |
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
| //Determine whether an integer is a palindrome. Do this without extra space. | |
| var isPalindrome = function (x) { | |
| if (x < 0) { | |
| return false; | |
| } | |
| //何桁で割ったら、1桁になるか | |
| var div = 1; |
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
| //antipattern | |
| var arr = new Array("apple", "google", "yahoo"); | |
| //the exact same array | |
| var arr = ["apple", "google", "yahoo"]; | |
| console.log(typeof arr); // "object" | |
| console.log(arr.constructor === Array); // true |
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 arr = new Array(2, 3, 4); | |
| console.log(arr); // [2, 3, 4] | |
| var arr2 = new Array(2); | |
| console.log(arr2); // [undefined, undefined] ew... | |
| console.log(arr2.length); //2 | |
| // using array literal | |
| var a = [3.14]; | |
| console.log( a[ 0]); // 3.14 |
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
| //javascript php foreach equivalent | |
| var obj = {prop1: 5, prop2: 13, prop3: 8}; | |
| for (var item in obj) { | |
| console.log(item + ':' + obj[item]); | |
| } |
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 parseQueryString = function(queryString) { | |
| var params = {}, | |
| queries, temp, i, l; | |
| queries = queryString.split('&'); | |
| l = queries.length; | |
| for (i = 0; i < l; i++) { | |
| temp = queries[i].split('='); |
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
| //before | |
| for (var i = 0, max = myarray.length; i < max; i + +) { | |
| // do something with myarray[ i] | |
| } | |
| //after | |
| var i, myarray = []; | |
| for (i = myarray.length; i--) { | |
| // do somthing with myarray[i] |
OlderNewer