-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path23-arrayPropertiesAndMethods.html
More file actions
95 lines (69 loc) · 3.54 KB
/
23-arrayPropertiesAndMethods.html
File metadata and controls
95 lines (69 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Demo</title>
</head>
<body>
<script type="text/javascript">
// Working with array properties and methods
/*
Properties: constructor, length and prototype
*/
var cars = ["Toyta", "Ford", "Chevy", "Dodge", "Hummer"];
var motorcycles = ["Harley-Davidson", "Indian", "BMW", "Buell", "Honda"]
document.write(cars.length + '<br>'); // using the .length property
var carsLength = cars.length;
document.write(carsLength + '<br>');
document.write(cars[carsLength - 1] + '<br>');
/*
To concatenate the two arrays, using the first object, use the method of
`.concat` and pass in the second object into the parens.
*/
var transportation = cars.concat(motorcycles);
document.write(transportation.length + '<br>');
document.write(transportation[5] + '<br>');
/*
See all properties and methods or an array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
and
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype#Methods
*/
// The join method - converts an array to a string
var officeFurniture = ['chair', 'desk', 'shelf', 'lamp']
var allOfficeFurniture = officeFurniture.join('; ');
document.write(allOfficeFurniture + '<br>');
// Fun with array methods
var officeFurnitureLength = officeFurniture.length; // get the array length
document.write(officeFurniture.length + '<br>'); // print value to the view
var officeFurnitureLastElement = officeFurniture.length - 1; // get length - 1
document.write(officeFurniture[officeFurnitureLastElement] + '<br>'); // print last element in the array to the view
officeFurniture.pop(); // removes the last element in the array
document.write(officeFurniture.length + '<br>');
var officeFurnitureLastElement = officeFurniture.length - 1;
document.write(officeFurniture[officeFurnitureLastElement] + '<br>');
officeFurniture.push("whiteboard"); // adds new element to the end of the array
document.write(officeFurniture.length + '<br>');
var officeFurnitureLastElement = officeFurniture.length - 1;
document.write(officeFurniture[officeFurnitureLastElement] + '<br>');
officeFurniture.reverse(); // reverses the order of the array
document.write(officeFurniture + '<br>');
officeFurniture.sort(); // alphabetical sorting of the array
document.write(officeFurniture + '<br>');
// Let's add array elements via an event loop
var shoppingList = [];
for (i = 0; i < 3; ++i) {
shoppingList[i] = prompt('Please enter your list', '');
}
document.write(shoppingList + '<br>');
// Print array elements
var fruit = ['apple', 'orange', 'pineapple', 'cherry', 'kiwi']
fruit.sort();
var fruitLength = fruit.length;
for (i = 0; i < fruitLength; ++i) {
document.write(fruit[i] + '<br>');
}
</script>
</body>
</html>