Skip to content

Commit 6ed6166

Browse files
committed
Converted 'running code' code examples to new convention
1 parent 5160f6b commit 6ed6166

File tree

2 files changed

+60
-39
lines changed

2 files changed

+60
-39
lines changed

page/javascript-101/arrays.md

+59-39
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ 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-
<javascript caption="A simple array">
15+
``` js
16+
// A simple array
1617
var myArray1 = new Array( 'hello', 'world' ); // with constructor
1718
var myArray2 = [ 'hello', 'world' ]; // literal declaration, the preferred way
18-
</javascript>
19+
```
1920

2021
The literal declaration is preferred, see the
2122
[Google Coding Guidelines](http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml#Array_and_Object_literals)
@@ -24,175 +25,192 @@ for more information.
2425
If you don't know your values yet, it is also possible to declare an empty Array, and
2526
add elements either through functions or through accessing by index:
2627

27-
<javascript caption="Creating empty arrays and adding values">
28+
29+
``` js
30+
// Creating empty arrays and adding values
2831
var myArray = [];
2932

3033
myArray.push('hello'); // adds 'hello' on index 0
3134
myArray.push('world'); // adds 'world' on index 1
3235
myArray[2] = '!'; // adds '!' on index 2
33-
</javascript>
36+
```
3437

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

39-
<javascript caption="Leaving indices">
42+
``` js
43+
// Leaving indices
4044
var myArray = [];
4145

4246
myArray[0] = 'hello';
4347
myArray[1] = 'world';
4448
myArray[3] = '!';
4549

4650
console.log(myArray); // logs ['hello', 'world', undefined, '!'];
47-
</javascript>
51+
```
4852

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

53-
<javascript caption="Accessing array items by index">
57+
``` js
58+
// Accessing array items by index
5459
var myArray = [ 'hello', 'world', '!'];
5560
console.log(myArray[2]); // logs '!'
56-
</javascript>
61+
```
5762

5863
### Array methods and properties
5964

6065
## length
6166

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

64-
<javascript caption="Length of an array">
69+
``` js
70+
// Length of an array
6571
var myArray = [ 'hello', 'world', '!'];
6672
console.log(myArray.length); // logs 3
67-
</javascript>
73+
```
6874

6975
You will need the length property for looping through an array:
7076

71-
<javascript caption="For loops and arrays - a classic">
77+
``` js
78+
// For loops and arrays - a classic
7279
var myArray = ['hello', 'world', '!'];
7380
for(var i = 0; i < myArray.length; i = i + 1) {
7481
console.log(myArray[i]);
7582
}
76-
</javascript>
83+
```
7784

7885
Except when you are using for ... in loops:
7986

80-
<javascript caption"For loops and arrays - alternate method">
87+
``` js
88+
// For loops and arrays - alternate method
8189
var myArray = ['hello', 'world', '!'];
8290
for(var i in myArray) {
8391
console.log(myArray[i]);
8492
}
85-
</javascript>
93+
```
8694

8795
## concat
8896

8997
With 'concat', you can concatenate two or more arrays
9098

91-
<javascript caption="Concatenating Arrays">
99+
``` js
100+
// Concatenating Arrays
92101
var myArray = [2, 3, 4];
93102
var myOtherArray = [5, 6, 7];
94103
var wholeArray = myArray.concat(myOtherArray); // [2, 3, 4, 5, 6, 7]
95-
</javascript>
104+
```
96105

97106
## join
98107

99108
'join' creates a string representation of your array. It's parameter is as string
100-
which works as a seperator between elements (default is a comma);
109+
which works as a separator between elements (default is a comma);
101110

102-
<javascript caption="Joining elements">
111+
``` js
112+
// Joining elements
103113
var myArray = ['hello', 'world', '!'];
104114
console.log(myArray.join(' ')); // logs "hello world !";
105115
console.log(myArray.join()); // logs "hello,world,!"
106116
console.log(myArray.join('')); // logs "helloworld!"
107117
console.log(myArray.join('!!')) // logs "hello!!world!!!!!";
108-
</javascript>
118+
```
109119

110120
## pop
111121

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

114-
<javascript caption="pushing and popping">
124+
``` js
125+
// pushing and popping
115126
var myArray = [];
116127
myArray.push(0); // [ 0 ]
117128
myArray.push(2); // [ 0 , 2 ]
118129
myArray.push(7); // [ 0 , 2 , 7 ]
119130
myArray.pop(); // [ 0 , 2 ]
120-
</javascript>
131+
```
121132

122133
## reverse
123134

124135
As the name suggests, the elements of the array are in reverse order after calling
125136
this method
126137

127-
<javascript caption="reverse">
138+
``` js
139+
// reverse
128140
var myArray = [ 'world' , 'hello' ];
129141
myArray.reverse(); // [ 'hello', 'world' ]
130-
</javascript>
142+
```
131143

132144
## shift
133145

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

137-
<javascript caption="queue with shift() and pop()">
149+
``` js
150+
// queue with shift() and pop()
138151
var myArray = [];
139152
myArray.push(0); // [ 0 ]
140153
myArray.push(2); // [ 0 , 2 ]
141154
myArray.push(7); // [ 0 , 2 , 7 ]
142155
myArray.shift(); // [ 2 , 7 ]
143-
</javascript>
156+
```
144157

145158
## slice
146159

147160
Extracts a part of the array and returns them in a new one. This method takes one
148161
parameter, which is the starting index.
149162

150-
<javascript caption="slicing">
163+
``` js
164+
// slicing
151165
var myArray = [1, 2, 3, 4, 5, 6, 7, 8];
152166
var newArray = myArray.slice(3);
153167

154168
console.log(myArray); // [1, 2, 3, 4, 5, 6, 7, 8]
155169
console.log(newArray); // [4, 5, 6, 7, 8]
156-
</javascript>
170+
```
157171

158172
## splice
159173

160174
Removes a certain amount of elements and adds new ones at the given index. It takes
161175
at least 3 parameters
162176

163-
<javascript caption="splice method">
177+
``` js
178+
// splice method
164179
myArray.splice(idx, len, values, ...);
165-
</javascript>
180+
```
166181

167182
* idx = the starting index
168183
* len = the number of elements to remove
169184
* values = the values which should be inserted at idx
170185

171186
For example:
172187

173-
<javascript caption="splice example">
188+
``` js
189+
// splice example
174190
var myArray = [0, 7, 8, 5];
175191
myArray.splice(1, 2, 1, 2, 3, 4);
176192
console.log(myArray); // [0, 1, 2, 3, 4, 5]
177-
</javascript>
193+
```
178194

179195
## sort
180196

181197
Sorts an array. It takes one parameter, which is a comparing function. If this function is not
182198
given, the array is sorted ascending
183199

184-
<javascript caption="sorting without comparing function">
200+
``` js
201+
// sorting without comparing function
185202
var myArray = [3, 4, 6, 1];
186203
myArray.sort(); // 1, 3, 4, 6
187-
</javascript>
204+
```
188205

189-
<javascript caption="sorting with comparing function">
206+
``` js
207+
// sorting with comparing function
190208
function descending(a, b) {
191209
return b - a;
192210
}
193211
var myArray = [3, 4, 6, 1];
194212
myArray.sort(descending); // [6, 4, 3, 1]
195-
</javascript>
213+
```
196214

197215
The return value of descending (for this example) is important. If the return value is
198216
less than zero, the index of a is before b, and if it is greater than zero it's vice-versa.
@@ -202,12 +220,13 @@ If the return value is zero, the elements index is equal.
202220

203221
Inserts an element at the first position of the array
204222

205-
<javascript caption="unshift">
223+
``` js
224+
// unshift
206225
var myArray = [];
207226
myArray.unshift(0); // [ 0 ]
208227
myArray.unshift(2); // [ 2 , 0 ]
209228
myArray.unshift(7); // [ 7 , 2 , 0 ]
210-
</javascript>
229+
```
211230

212231
## forEach
213232

@@ -222,7 +241,8 @@ The function takes up to three arguments:
222241

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

225-
<javascript caption="native forEach">
244+
``` js
245+
// native forEach
226246
function printElement(elem) {
227247
console.log(elem);
228248
}
@@ -239,4 +259,4 @@ myArray = [1, 2, 3, 4, 5];
239259
myArray.forEach(printElement); //prints all elements to the console
240260
myArray.forEach(printElementAndIndex); //prints "Index 0: 1" "Index 1: 2" "Index 2: 3" ...
241261
myArray.forEach(negateElement); // myArray is now [-1, -2, -3, -4, -5]
242-
</javascript>
262+
```

page/javascript-101/running-code.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The first and recommended option is to write our code in an external file (with
1212
// Saved in example.js.
1313
alert('Hello World!');
1414
```
15+
1516
``` html
1617
<!--Code is then included via the script tag src attribute.-->
1718
<script src="/path/to/example.js"></script>

0 commit comments

Comments
 (0)