Skip to content

Commit dc382a1

Browse files
bobholtajpiano
authored andcommitted
apply core styles to examples in javascript-101
1 parent 157d7b1 commit dc382a1

14 files changed

+687
-472
lines changed

page/javascript-101/arrays.md

+122-83
Original file line numberDiff line numberDiff line change
@@ -12,52 +12,54 @@ can include multiple types of items, including other arrays.
1212
To create an array you can either use the object constructor or the literal declaration,
1313
by assigning your variable a list of values right after the declaration.
1414

15-
``` js
15+
```
1616
// A simple array
17-
var myArray1 = new Array( 'hello', 'world' ); // with constructor
18-
var myArray2 = [ 'hello', 'world' ]; // literal declaration, the preferred way
17+
var myArray1 = new Array( "hello", "world" ); // with constructor
18+
var myArray2 = [ "hello", "world" ]; // literal declaration, the preferred way
1919
```
2020

21-
The literal declaration is preferred, see the
21+
The literal declaration is preferred, see the
2222
[Google Coding Guidelines](http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml#Array_and_Object_literals)
2323
for more information.
2424

2525
If you don't know your values yet, it is also possible to declare an empty Array, and
2626
add elements either through functions or through accessing by index:
2727

2828

29-
``` js
29+
```
3030
// Creating empty arrays and adding values
3131
var myArray = [];
3232
33-
myArray.push('hello'); // adds 'hello' on index 0
34-
myArray.push('world'); // adds 'world' on index 1
35-
myArray[2] = '!'; // adds '!' on index 2
33+
myArray.push("hello"); // adds "hello" on index 0
34+
35+
myArray.push("world"); // adds "world" on index 1
36+
37+
myArray[ 2 ] = "!"; // adds "!" on index 2
3638
```
3739

3840
'push' is a function which adds an element on the end of the array and expands the array
3941
respectively. You also can directly add items by index. Missing indices will be filled
4042
with 'undefined';
4143

42-
``` js
44+
```
4345
// Leaving indices
4446
var myArray = [];
4547
46-
myArray[0] = 'hello';
47-
myArray[1] = 'world';
48-
myArray[3] = '!';
48+
myArray[ 0 ] = "hello";
49+
myArray[ 1 ] = "world";
50+
myArray[ 3 ] = "!";
4951
50-
console.log(myArray); // logs ['hello', 'world', undefined, '!'];
52+
console.log( myArray ); // logs [ "hello", "world", undefined, "!" ];
5153
```
5254

5355
So 'push' is far more safe, especially if you don't know the size of your
5456
array yet. With the index you not only assign values to array items, but also
5557
access those.
5658

57-
``` js
59+
```
5860
// Accessing array items by index
59-
var myArray = [ 'hello', 'world', '!'];
60-
console.log(myArray[2]); // logs '!'
61+
var myArray = [ "hello", "world", "!" ];
62+
console.log( myArray[2] ); // logs "!"
6163
```
6264

6365
### Array methods and properties
@@ -66,92 +68,112 @@ console.log(myArray[2]); // logs '!'
6668

6769
The 'length' property is used to know the amount of items in your array.
6870

69-
``` js
71+
```
7072
// Length of an array
71-
var myArray = [ 'hello', 'world', '!'];
72-
console.log(myArray.length); // logs 3
73+
var myArray = [ "hello", "world", "!" ];
74+
75+
console.log( myArray.length ); // logs 3
7376
```
7477

7578
You will need the length property for looping through an array:
7679

77-
``` js
80+
```
7881
// For loops and arrays - a classic
79-
var myArray = ['hello', 'world', '!'];
80-
for(var i = 0; i < myArray.length; i = i + 1) {
81-
console.log(myArray[i]);
82+
var myArray = [ "hello", "world", "!" ];
83+
84+
for ( var i = 0; i < myArray.length; i = i + 1 ) {
85+
86+
console.log( myArray[i] );
87+
8288
}
8389
```
8490

8591
Except when you are using for ... in loops:
8692

87-
``` js
93+
```
8894
// For loops and arrays - alternate method
89-
var myArray = ['hello', 'world', '!'];
90-
for(var i in myArray) {
91-
console.log(myArray[i]);
95+
var myArray = ["hello", "world", "!"];
96+
97+
for ( var i in myArray ) {
98+
99+
console.log( myArray[i] );
100+
92101
}
93102
```
94103

95104
## concat
96105

97106
With 'concat', you can concatenate two or more arrays
98107

99-
``` js
108+
```
100109
// Concatenating Arrays
101-
var myArray = [2, 3, 4];
102-
var myOtherArray = [5, 6, 7];
103-
var wholeArray = myArray.concat(myOtherArray); // [2, 3, 4, 5, 6, 7]
110+
var myArray = [ 2, 3, 4 ];
111+
var myOtherArray = [ 5, 6, 7 ];
112+
var wholeArray = myArray.concat( myOtherArray ); // [ 2, 3, 4, 5, 6, 7 ]
104113
```
105114

106115
## join
107116

108117
'join' creates a string representation of your array. It's parameter is as string
109118
which works as a separator between elements (default is a comma);
110119

111-
``` js
120+
```
112121
// Joining elements
113-
var myArray = ['hello', 'world', '!'];
114-
console.log(myArray.join(' ')); // logs "hello world !";
115-
console.log(myArray.join()); // logs "hello,world,!"
116-
console.log(myArray.join('')); // logs "helloworld!"
117-
console.log(myArray.join('!!')) // logs "hello!!world!!!!!";
122+
var myArray = [ "hello", "world", "!" ];
123+
124+
console.log( myArray.join(" ") ); // logs "hello world !";
125+
126+
console.log( myArray.join() ); // logs "hello,world,!"
127+
128+
console.log( myArray.join("") ); // logs "helloworld!"
129+
130+
console.log( myArray.join("!!") ); // logs "hello!!world!!!!!";
118131
```
119132

120133
## pop
121134

122135
'pop' removes the last element of an array. It is the opposite method to 'push'
123136

124-
``` js
137+
```
125138
// pushing and popping
126139
var myArray = [];
127-
myArray.push(0); // [ 0 ]
128-
myArray.push(2); // [ 0 , 2 ]
129-
myArray.push(7); // [ 0 , 2 , 7 ]
130-
myArray.pop(); // [ 0 , 2 ]
140+
141+
myArray.push( 0 ); // [ 0 ]
142+
143+
myArray.push( 2 ); // [ 0 , 2 ]
144+
145+
myArray.push( 7 ); // [ 0 , 2 , 7 ]
146+
147+
myArray.pop(); // [ 0 , 2 ]
131148
```
132149

133-
## reverse
150+
## reverse
134151

135152
As the name suggests, the elements of the array are in reverse order after calling
136153
this method
137154

138-
``` js
155+
```
139156
// reverse
140-
var myArray = [ 'world' , 'hello' ];
141-
myArray.reverse(); // [ 'hello', 'world' ]
157+
var myArray = [ "world" , "hello" ];
158+
159+
myArray.reverse(); // [ "hello", "world" ]
142160
```
143161

144162
## shift
145163

146164
Removes the first element of an array. With 'pop' and 'shift' you can recreate the
147165
method of a [queue](http://en.wikipedia.org/wiki/Queue_(data_structure))
148166

149-
``` js
167+
```
150168
// queue with shift() and pop()
151169
var myArray = [];
152-
myArray.push(0); // [ 0 ]
153-
myArray.push(2); // [ 0 , 2 ]
154-
myArray.push(7); // [ 0 , 2 , 7 ]
170+
171+
myArray.push( 0 ); // [ 0 ]
172+
173+
myArray.push( 2 ); // [ 0 , 2 ]
174+
175+
myArray.push( 7 ); // [ 0 , 2 , 7 ]
176+
155177
myArray.shift(); // [ 2 , 7 ]
156178
```
157179

@@ -160,23 +182,23 @@ myArray.shift(); // [ 2 , 7 ]
160182
Extracts a part of the array and returns them in a new one. This method takes one
161183
parameter, which is the starting index.
162184

163-
``` js
185+
```
164186
// slicing
165-
var myArray = [1, 2, 3, 4, 5, 6, 7, 8];
166-
var newArray = myArray.slice(3);
187+
var myArray = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
188+
var newArray = myArray.slice( 3 );
167189
168-
console.log(myArray); // [1, 2, 3, 4, 5, 6, 7, 8]
169-
console.log(newArray); // [4, 5, 6, 7, 8]
190+
console.log( myArray ); // [ 1, 2, 3, 4, 5, 6, 7, 8 ]
191+
console.log( newArray ); // [ 4, 5, 6, 7, 8 ]
170192
```
171193

172194
## splice
173195

174196
Removes a certain amount of elements and adds new ones at the given index. It takes
175197
at least 3 parameters
176198

177-
``` js
199+
```
178200
// splice method
179-
myArray.splice(idx, len, values, ...);
201+
myArray.splice( idx, len, values, ... );
180202
```
181203

182204
* idx = the starting index
@@ -185,31 +207,36 @@ myArray.splice(idx, len, values, ...);
185207

186208
For example:
187209

188-
``` js
210+
```
189211
// splice example
190-
var myArray = [0, 7, 8, 5];
191-
myArray.splice(1, 2, 1, 2, 3, 4);
192-
console.log(myArray); // [0, 1, 2, 3, 4, 5]
212+
var myArray = [ 0, 7, 8, 5 ];
213+
myArray.splice( 1, 2, 1, 2, 3, 4 );
214+
console.log( myArray ); // [ 0, 1, 2, 3, 4, 5 ]
193215
```
194216

195217
## sort
196218

197219
Sorts an array. It takes one parameter, which is a comparing function. If this function is not
198220
given, the array is sorted ascending
199221

200-
``` js
222+
```
201223
// sorting without comparing function
202-
var myArray = [3, 4, 6, 1];
224+
var myArray = [ 3, 4, 6, 1 ];
225+
203226
myArray.sort(); // 1, 3, 4, 6
204227
```
205228

206-
``` js
229+
```
207230
// sorting with comparing function
208-
function descending(a, b) {
231+
function descending( a, b ) {
232+
209233
return b - a;
234+
210235
}
211-
var myArray = [3, 4, 6, 1];
212-
myArray.sort(descending); // [6, 4, 3, 1]
236+
237+
var myArray = [ 3, 4, 6, 1 ];
238+
239+
myArray.sort( descending ); // [ 6, 4, 3, 1 ]
213240
```
214241

215242
The return value of descending (for this example) is important. If the return value is
@@ -220,17 +247,20 @@ If the return value is zero, the elements index is equal.
220247

221248
Inserts an element at the first position of the array
222249

223-
``` js
250+
```
224251
// unshift
225252
var myArray = [];
226-
myArray.unshift(0); // [ 0 ]
227-
myArray.unshift(2); // [ 2 , 0 ]
228-
myArray.unshift(7); // [ 7 , 2 , 0 ]
253+
254+
myArray.unshift( 0 ); // [ 0 ]
255+
256+
myArray.unshift( 2 ); // [ 2 , 0 ]
257+
258+
myArray.unshift( 7 ); // [ 7 , 2 , 0 ]
229259
```
230260

231261
## forEach
232262

233-
In modern browsers, like Chrome, Firefox and Internet Explorer 9 it is possible to traverse
263+
In modern browsers, like Chrome, Firefox and Internet Explorer 9 it is possible to traverse
234264
through arrays by a so called 'forEach' method, where you pass a function which is called
235265
for each element in your array.
236266

@@ -241,22 +271,31 @@ The function takes up to three arguments:
241271

242272
All of the are optional, but you will need at least the 'element' parameter in most cases.
243273

244-
``` js
274+
```
245275
// native forEach
246-
function printElement(elem) {
247-
console.log(elem);
276+
function printElement( elem ) {
277+
278+
console.log( elem );
279+
248280
}
249281
250-
function printElementAndIndex(elem, index) {
251-
console.log("Index " + index + ": " + elem);
282+
function printElementAndIndex( elem, index ) {
283+
284+
console.log( "Index " + index + ": " + elem );
285+
252286
}
253287
254-
function negateElement(elem, index, array) {
255-
array[index] = -elem;
288+
function negateElement( elem, index, array ) {
289+
290+
array[ index ] = -elem;
291+
256292
}
257293
258-
myArray = [1, 2, 3, 4, 5];
259-
myArray.forEach(printElement); //prints all elements to the console
260-
myArray.forEach(printElementAndIndex); //prints "Index 0: 1" "Index 1: 2" "Index 2: 3" ...
261-
myArray.forEach(negateElement); // myArray is now [-1, -2, -3, -4, -5]
294+
myArray = [ 1, 2, 3, 4, 5 ];
295+
296+
myArray.forEach( printElement ); //prints all elements to the console
297+
298+
myArray.forEach( printElementAndIndex ); //prints "Index 0: 1" "Index 1: 2" "Index 2: 3" ...
299+
300+
myArray.forEach( negateElement ); // myArray is now [ -1, -2, -3, -4, -5 ]
262301
```

0 commit comments

Comments
 (0)