From e5d14c1ded98bb041e5ca0a87844474e4db96bd0 Mon Sep 17 00:00:00 2001 From: Markus Amalthea Magnuson Date: Thu, 18 Apr 2013 13:23:40 +0200 Subject: [PATCH 1/9] Some space changes. --- page/javascript-101/arrays.md | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/page/javascript-101/arrays.md b/page/javascript-101/arrays.md index ac2dffe5..26c5db7f 100644 --- a/page/javascript-101/arrays.md +++ b/page/javascript-101/arrays.md @@ -5,6 +5,7 @@ source: http://jqfundamentals.com/legacy attribution: - jQuery Fundamentals --- + Arrays are zero-indexed, ordered lists of values. They are a handy way to store a set of related items of the same type (such as strings), though in reality, an array can include multiple types of items, including other arrays. To create an array, either use the object constructor or the literal declaration, by assigning the variable a list of values after the declaration. @@ -12,6 +13,7 @@ To create an array, either use the object constructor or the literal declaration ``` // A simple array with constructor. var myArray1 = new Array( "hello", "world" ); + // Literal declaration, the preferred way. var myArray2 = [ "hello", "world" ]; ``` @@ -22,6 +24,7 @@ If the values are unknown, it is also possible to declare an empty array, and ad ``` // Creating empty arrays and adding values + var myArray = []; // adds "hello" on index 0 @@ -38,6 +41,7 @@ myArray[ 2 ] = "!"; ``` // Leaving indices + var myArray = []; myArray[ 0 ] = "hello"; @@ -51,6 +55,7 @@ If the size of the array is unknown, `.push()` is far more safe. You can both ac ``` // Accessing array items by index + var myArray = [ "hello", "world", "!" ]; console.log( myArray[2] ); // "!" @@ -64,6 +69,7 @@ The `.length` property is used to determine the amount of items in an array. ``` // Length of an array + var myArray = [ "hello", "world", "!" ]; console.log( myArray.length ); // 3 @@ -73,10 +79,13 @@ You will need the `.length` property for looping through an array: ``` // For loops and arrays - a classic + var myArray = [ "hello", "world", "!" ]; for ( var i = 0; i < myArray.length; i = i + 1 ) { console.log( myArray[i] ); + + } ``` @@ -84,10 +93,13 @@ Except when using `for`/`in` loops: ``` // For loops and arrays - alternate method + var myArray = [ "hello", "world", "!" ]; for ( var i in myArray ) { + console.log( myArray[ i ] ); + } ``` @@ -99,7 +111,6 @@ Concatenate two or more arrays with `.concat()`: // Concatenating Arrays var myArray = [ 2, 3, 4 ]; var myOtherArray = [ 5, 6, 7 ]; - // [ 2, 3, 4, 5, 6, 7 ] var wholeArray = myArray.concat( myOtherArray ); ``` @@ -110,6 +121,7 @@ var wholeArray = myArray.concat( myOtherArray ); ``` // Joining elements + var myArray = [ "hello", "world", "!" ]; // The default separator is a comma @@ -130,6 +142,7 @@ console.log( myArray.join("") ); // "helloworld!" ``` // pushing and popping + var myArray = []; myArray.push( 0 ); // [ 0 ] @@ -145,7 +158,6 @@ As the name suggests, the elements of the array are in reverse order after calli ``` // reverse var myArray = [ "world" , "hello" ]; - // [ "hello", "world" ] myArray.reverse(); ``` @@ -156,6 +168,7 @@ Removes the first element of an array. With `.push()` and `.shift()`, you can re ``` // queue with shift() and push() + var myArray = []; myArray.push( 0 ); // [ 0 ] @@ -170,6 +183,7 @@ Extracts a part of the array and returns that part in a new array. This method t ``` // slicing + var myArray = [ 1, 2, 3, 4, 5, 6, 7, 8 ]; var newArray = myArray.slice( 3 ); @@ -206,6 +220,7 @@ Sorts an array. It takes one parameter, which is a comparing function. If this f ``` // sorting without comparing function + var myArray = [ 3, 4, 6, 1 ]; myArray.sort(); // 1, 3, 4, 6 @@ -213,6 +228,7 @@ myArray.sort(); // 1, 3, 4, 6 ``` // sorting with comparing function + function descending( a, b ) { return b - a; } @@ -251,6 +267,7 @@ All of these are optional, but you will need at least the "Element" parameter in ``` // native forEach + function printElement( elem ) { console.log( elem ); } From 5ec45c5de6bd09f19a8c195a4513970225262811 Mon Sep 17 00:00:00 2001 From: Markus Amalthea Magnuson Date: Thu, 18 Apr 2013 13:28:23 +0200 Subject: [PATCH 2/9] Sentencize code comments. --- page/javascript-101/arrays.md | 38 +++++++++++++++++------------------ 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/page/javascript-101/arrays.md b/page/javascript-101/arrays.md index 26c5db7f..51dc283d 100644 --- a/page/javascript-101/arrays.md +++ b/page/javascript-101/arrays.md @@ -23,24 +23,24 @@ The literal declaration is generally preferred. See the [Google Coding Guideline If the values are unknown, it is also possible to declare an empty array, and add elements either through functions or through accessing by index: ``` -// Creating empty arrays and adding values +// Creating empty arrays and adding values. var myArray = []; -// adds "hello" on index 0 +// Adds "hello" on index 0 myArray.push( "hello" ); -// adds "world" on index 1 +// Adds "world" on index 1 myArray.push( "world" ); -// adds "!" on index 2 +// Adds "!" on index 2 myArray[ 2 ] = "!"; ``` `.push()` is a function that adds an element on the end of the array and expands the array respectively. You also can directly add items by index. Missing indices will be filled with `undefined`. ``` -// Leaving indices +// Leaving indices. var myArray = []; @@ -54,7 +54,7 @@ console.log( myArray ); // [ "hello", "world", undefined, "!" ]; If the size of the array is unknown, `.push()` is far more safe. You can both access and assign values to array items with the index. ``` -// Accessing array items by index +// Accessing array items by index. var myArray = [ "hello", "world", "!" ]; @@ -68,7 +68,7 @@ console.log( myArray[2] ); // "!" The `.length` property is used to determine the amount of items in an array. ``` -// Length of an array +// Length of an array. var myArray = [ "hello", "world", "!" ]; @@ -78,7 +78,7 @@ console.log( myArray.length ); // 3 You will need the `.length` property for looping through an array: ``` -// For loops and arrays - a classic +// For loops and arrays - a classic. var myArray = [ "hello", "world", "!" ]; @@ -92,7 +92,7 @@ for ( var i = 0; i < myArray.length; i = i + 1 ) { Except when using `for`/`in` loops: ``` -// For loops and arrays - alternate method +// For loops and arrays - alternate method. var myArray = [ "hello", "world", "!" ]; @@ -120,11 +120,11 @@ var wholeArray = myArray.concat( myOtherArray ); `.join()` creates a string representation of an array by joining all of its elements using a separator string. If no separator is supplied (e.g. `.join()` is called without arguments) the array will be joined using a comma: ``` -// Joining elements +// Joining elements. var myArray = [ "hello", "world", "!" ]; -// The default separator is a comma +// The default separator is a comma. console.log( myArray.join() ); // "hello,world,!" // Any string can be used as separator... @@ -141,7 +141,7 @@ console.log( myArray.join("") ); // "helloworld!" `.pop()` removes the last element of an array. It is the opposite method of `.push()`: ``` -// pushing and popping +// Pushing and popping. var myArray = []; @@ -167,7 +167,7 @@ myArray.reverse(); Removes the first element of an array. With `.push()` and `.shift()`, you can recreate the method of a [queue](http://en.wikipedia.org/wiki/Queue_(data_structure)): ``` -// queue with shift() and push() +// Queue with shift() and push() var myArray = []; @@ -182,7 +182,7 @@ myArray.shift(); // [ 2 , 7 ] Extracts a part of the array and returns that part in a new array. This method takes one parameter, which is the starting index: ``` -// slicing +// Slicing. var myArray = [ 1, 2, 3, 4, 5, 6, 7, 8 ]; var newArray = myArray.slice( 3 ); @@ -219,7 +219,7 @@ console.log( myArray ); // [ 0, 1, 2, 3, 4, 5 ] Sorts an array. It takes one parameter, which is a comparing function. If this function is not given, the array is sorted ascending: ``` -// sorting without comparing function +// Sorting without comparing function. var myArray = [ 3, 4, 6, 1 ]; @@ -227,7 +227,7 @@ myArray.sort(); // 1, 3, 4, 6 ``` ``` -// sorting with comparing function +// Sorting with comparing function. function descending( a, b ) { return b - a; @@ -266,7 +266,7 @@ The function takes up to three arguments: All of these are optional, but you will need at least the "Element" parameter in most cases. ``` -// native forEach +// Native .forEach() function printElement( elem ) { console.log( elem ); @@ -282,10 +282,10 @@ function negateElement( elem, index, array ) { myArray = [ 1, 2, 3, 4, 5 ]; -// prints all elements to the console +// Prints all elements to the console. myArray.forEach( printElement ); -// prints "Index 0: 1" "Index 1: 2" "Index 2: 3" ... +// Prints "Index 0: 1", "Index 1: 2", "Index 2: 3", ... myArray.forEach( printElementAndIndex ); // myArray is now [ -1, -2, -3, -4, -5 ] From 6373cdfb1bb69a1a67f2ca5351526cdcb25f8974 Mon Sep 17 00:00:00 2001 From: Markus Amalthea Magnuson Date: Thu, 18 Apr 2013 13:28:58 +0200 Subject: [PATCH 3/9] Remove redundant code comments. The exact same thing is said right befor the code block. --- page/javascript-101/arrays.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/page/javascript-101/arrays.md b/page/javascript-101/arrays.md index 51dc283d..ce030b26 100644 --- a/page/javascript-101/arrays.md +++ b/page/javascript-101/arrays.md @@ -108,7 +108,6 @@ for ( var i in myArray ) { Concatenate two or more arrays with `.concat()`: ``` -// Concatenating Arrays var myArray = [ 2, 3, 4 ]; var myOtherArray = [ 5, 6, 7 ]; // [ 2, 3, 4, 5, 6, 7 ] @@ -156,7 +155,6 @@ myArray.pop(); // [ 0 , 2 ] As the name suggests, the elements of the array are in reverse order after calling this method: ``` -// reverse var myArray = [ "world" , "hello" ]; // [ "hello", "world" ] myArray.reverse(); @@ -196,7 +194,6 @@ console.log( newArray ); // [ 4, 5, 6, 7, 8 ] Removes a certain amount of elements and adds new ones at the given index. It takes at least three parameters: ``` -// splice method myArray.splice( index, length, values, ... ); ``` @@ -207,7 +204,6 @@ myArray.splice( index, length, values, ... ); For example: ``` -// splice example var myArray = [ 0, 7, 8, 5 ]; myArray.splice( 1, 2, 1, 2, 3, 4 ); @@ -245,7 +241,6 @@ The return value of descending (for this example) is important. If the return va Inserts an element at the first position of the array: ``` -// unshift var myArray = []; myArray.unshift( 0 ); // [ 0 ] From 6c1a040a22f1cd95acea22d628fe603e431d5e06 Mon Sep 17 00:00:00 2001 From: Markus Amalthea Magnuson Date: Thu, 18 Apr 2013 13:29:34 +0200 Subject: [PATCH 4/9] Reformat code, add spaces. --- page/javascript-101/arrays.md | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/page/javascript-101/arrays.md b/page/javascript-101/arrays.md index ce030b26..11e9dd2f 100644 --- a/page/javascript-101/arrays.md +++ b/page/javascript-101/arrays.md @@ -58,7 +58,7 @@ If the size of the array is unknown, `.push()` is far more safe. You can both ac var myArray = [ "hello", "world", "!" ]; -console.log( myArray[2] ); // "!" +console.log( myArray[ 2 ] ); // "!" ``` ## Array Methods and Properties @@ -83,8 +83,8 @@ You will need the `.length` property for looping through an array: var myArray = [ "hello", "world", "!" ]; for ( var i = 0; i < myArray.length; i = i + 1 ) { - console.log( myArray[i] ); + console.log( myArray[ i ] ); } ``` @@ -110,8 +110,7 @@ Concatenate two or more arrays with `.concat()`: ``` var myArray = [ 2, 3, 4 ]; var myOtherArray = [ 5, 6, 7 ]; -// [ 2, 3, 4, 5, 6, 7 ] -var wholeArray = myArray.concat( myOtherArray ); +var wholeArray = myArray.concat( myOtherArray ); // [ 2, 3, 4, 5, 6, 7 ] ``` ### .join() @@ -127,11 +126,11 @@ var myArray = [ "hello", "world", "!" ]; console.log( myArray.join() ); // "hello,world,!" // Any string can be used as separator... -console.log( myArray.join(" ") ); // "hello world !"; -console.log( myArray.join("!!") ); // "hello!!world!!!"; +console.log( myArray.join( " " ) ); // "hello world !"; +console.log( myArray.join( "!!" ) ); // "hello!!world!!!"; -// ...including an empty one -console.log( myArray.join("") ); // "helloworld!" +// ...including an empty one. +console.log( myArray.join( "" ) ); // "helloworld!" ``` @@ -156,8 +155,7 @@ As the name suggests, the elements of the array are in reverse order after calli ``` var myArray = [ "world" , "hello" ]; -// [ "hello", "world" ] -myArray.reverse(); +myArray.reverse(); // [ "hello", "world" ] ``` ### .shift() From 65813128891d8922720868a0c88530333c91d2dc Mon Sep 17 00:00:00 2001 From: Markus Amalthea Magnuson Date: Thu, 18 Apr 2013 13:29:47 +0200 Subject: [PATCH 5/9] Add colon. --- page/javascript-101/arrays.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/page/javascript-101/arrays.md b/page/javascript-101/arrays.md index 11e9dd2f..57a28c7f 100644 --- a/page/javascript-101/arrays.md +++ b/page/javascript-101/arrays.md @@ -115,7 +115,7 @@ var wholeArray = myArray.concat( myOtherArray ); // [ 2, 3, 4, 5, 6, 7 ] ### .join() -`.join()` creates a string representation of an array by joining all of its elements using a separator string. If no separator is supplied (e.g. `.join()` is called without arguments) the array will be joined using a comma: +`.join()` creates a string representation of an array by joining all of its elements using a separator string. If no separator is supplied (e.g. `.join()` is called without arguments) the array will be joined using a comma. ``` // Joining elements. From 5ba72e34bb1b280a03133850eefa33c5db406982 Mon Sep 17 00:00:00 2001 From: Markus Amalthea Magnuson Date: Thu, 18 Apr 2013 13:30:32 +0200 Subject: [PATCH 6/9] Fix broken link. URLs ending with parenthesis are mangled, this is not a beautiful fix but at least it works. --- page/javascript-101/arrays.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/page/javascript-101/arrays.md b/page/javascript-101/arrays.md index 57a28c7f..6289a5ae 100644 --- a/page/javascript-101/arrays.md +++ b/page/javascript-101/arrays.md @@ -160,7 +160,7 @@ myArray.reverse(); // [ "hello", "world" ] ### .shift() -Removes the first element of an array. With `.push()` and `.shift()`, you can recreate the method of a [queue](http://en.wikipedia.org/wiki/Queue_(data_structure)): +Removes the first element of an array. With `.push()` and `.shift()`, you can recreate the method of a [queue](http://en.wikipedia.org/wiki/Queue_(abstract_data_type%29): ``` // Queue with shift() and push() From a5cae8aa39f7402f3e839c430f902bcd24dc2b38 Mon Sep 17 00:00:00 2001 From: Markus Amalthea Magnuson Date: Thu, 18 Apr 2013 13:30:44 +0200 Subject: [PATCH 7/9] Hyphens to en dashes. --- page/javascript-101/arrays.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/page/javascript-101/arrays.md b/page/javascript-101/arrays.md index 6289a5ae..8ec394e2 100644 --- a/page/javascript-101/arrays.md +++ b/page/javascript-101/arrays.md @@ -195,9 +195,9 @@ Removes a certain amount of elements and adds new ones at the given index. It ta myArray.splice( index, length, values, ... ); ``` -* *Index* - The starting index. -* *Length* - The number of elements to remove. -* *Values* - The values to be inserted at the Index position. +* *Index* – The starting index. +* *Length* – The number of elements to remove. +* *Values* – The values to be inserted at the index position. For example: @@ -252,9 +252,9 @@ In modern browsers it is possible to traverse through arrays with a `.forEach()` The function takes up to three arguments: -* *Element* - The element itself. -* *Index* - The index of this element in the array. -* *Array* - The array itself. +* *Element* – The element itself. +* *Index* – The index of this element in the array. +* *Array* – The array itself. All of these are optional, but you will need at least the "Element" parameter in most cases. From f424d9550d6e1f07a36b8523f6433ccb032e6070 Mon Sep 17 00:00:00 2001 From: Markus Amalthea Magnuson Date: Thu, 18 Apr 2013 13:31:05 +0200 Subject: [PATCH 8/9] a/b -> `a`/`b` --- page/javascript-101/arrays.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/page/javascript-101/arrays.md b/page/javascript-101/arrays.md index 8ec394e2..994dbcdb 100644 --- a/page/javascript-101/arrays.md +++ b/page/javascript-101/arrays.md @@ -232,7 +232,7 @@ var myArray = [ 3, 4, 6, 1 ]; myArray.sort( descending ); // [ 6, 4, 3, 1 ] ``` -The return value of descending (for this example) is important. If the return value is less than zero, the index of a is before b, and if it is greater than zero it's vice-versa. If the return value is zero, the elements index is equal. +The return value of descending (for this example) is important. If the return value is less than zero, the index of `a` is before `b`, and if it is greater than zero it's vice-versa. If the return value is zero, the elements' index is equal. ### .unshift() From 27048ee5bf893e8ef41cd9e8ea92d7034f774d60 Mon Sep 17 00:00:00 2001 From: Markus Amalthea Magnuson Date: Thu, 18 Apr 2013 13:31:34 +0200 Subject: [PATCH 9/9] Emphasis instead of quotation marks. To more resemble the previous mention of Element. --- page/javascript-101/arrays.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/page/javascript-101/arrays.md b/page/javascript-101/arrays.md index 994dbcdb..c58208b4 100644 --- a/page/javascript-101/arrays.md +++ b/page/javascript-101/arrays.md @@ -256,7 +256,7 @@ The function takes up to three arguments: * *Index* – The index of this element in the array. * *Array* – The array itself. -All of these are optional, but you will need at least the "Element" parameter in most cases. +All of these are optional, but you will need at least the *Element* parameter in most cases. ``` // Native .forEach()