This file contains 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
from https://coderwall.com/p/nilaba/simple-pure-javascript-array-unique-method-with-5-lines-of-code | |
Array.prototype.unique = function() { | |
return this.filter(function (value, index, self) { | |
return self.indexOf(value) === index; | |
}); | |
} | |
var arr = ['a', 1, 'a', 2, '1'] | |
arr.unique(); // => ['a', 1, 2, '1'] |
This file contains 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
use switch statement when if else statements accumulate | |
var expr = 'Oranges'; | |
switch (expr) { | |
case "Oranges": | |
console.log("Oranges are $0.59 a pound."); | |
break; | |
case "Apples": | |
console.log("Apples are $0.32 a pound."); | |
break; |
This file contains 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 age = Number(prompt('What is your age? ', ' ')); | |
var string = ''; | |
for (var counter = 1; counter < age; counter++) { | |
string += 'Happy Birthday\n'; | |
if (counter % 7 === 0) { | |
break; | |
} | |
} |
This file contains 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 str = 'my name is liz'; | |
str.length // returns 14 | |
charAt(index) - returns single char of a string at index postion | |
str.charAt(0); // returns "m" | |
str.charAt(3); // returns "n" | |
indexOf(string) | |
str.indexOf("n"); // returns 3 |
This file contains 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 range(max) { | |
var retVal = []; | |
for (var i = 0; i < max; i++) { | |
retVal.push(i * 2); | |
} | |
return retVal; | |
} | |
var myArray = range(5); |
This file contains 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 input element with a type attribute whose value is "file" represents a list of file items, each consisting of a file name, a file type, and a file body (the contents of the file). | |
http://www.w3.org/TR/html-markup/input.file.html | |
http://msdn.microsoft.com/en-us/library/windows/apps/hh466145.aspx | |
Read Text Files Using the JavaScript FileReader | |
http://www.htmlgoodies.com/beyond/javascript/read-text-files-using-the-javascript-filereader.html | |
READONLY information for an individual file such as its name, size, mimetype, and a reference to the file handle. Adding the "multiple" attribute to the file input element (as in <input type="file" multiple/> will get you a list of files | |
<input type="file"/> | |
input always returns a FileList object |
This file contains 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
for(in) loops through the enumerable properties of an object, not the indexes of an array | |
console.log('The primary use case for for(in) is to enumerate the properties of an object and will result in unexpected results if used with an Array'); | |
console.log('for(in) gives you the index positions of the items if used in the array and not an objecct:'); | |
console.log('the results below are from using a for(in) with var myNewArray = ["a", "b", "c", "d"]'); | |
var myNewArray = ["a", "b", "c", "d"]; | |
for (var i in myNewArray) { | |
console.log('i', i); |
This file contains 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
from: http://stackoverflow.com/questions/9329446/how-to-do-for-each-over-an-array-in-javascript | |
For Actual Arrays | |
(See "For Array-Like Objects" below for array-like objects.) | |
You currently have three options and will soon have two more: | |
Use forEach and related (ES5+) | |
Use a simple for loop |
This file contains 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
.. |
This file contains 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
// arr.forEach(callback[, thisArg]) | |
// REMEMBER!! The forEach uses a function | |
// The forEach() method executes a provided function once per array element. | |
// The function can be written outside of the forEach() and called in the forEach() or | |
// the function can be written inside of the forEach. | |
// You cannot break from a forEach(). | |
// Each of the 3 code blocks below uses the same function written inside of the forEach(). | |
// also these functions were between script tags in the .html file, if in ext file, they would need to be slightly modified using the this keyword | |
// For performance-critical scenarios (“hot paths,” tight loops, etc.), a classic for loop is better because the Javascript interpreter can better optimize it. This is also necessary if you need to break from the loop, which you cannot do with forEach. | |
// http://www.kraigbrockschmidt.com/2013/01/22/array-enumeration-javascript/ |
NewerOlder