-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-intro.html
More file actions
251 lines (181 loc) · 6.66 KB
/
Copy patharray-intro.html
File metadata and controls
251 lines (181 loc) · 6.66 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
</title>
</head>
<body>
<script>
"use strict";
// ============================= Arrays Definition
/*
In JS, an array is a special object data type that may contain
a collection of multiple data types at numeric indices.
In other words, an array allows us to store and access multiple values (elements)
through one variable name (array name) and a specific index number.
Array of animals...
_______ _______ _______ _______
| || || || |
| DOG || CAT || EGG || EMU |
| || || || |
------- ------- ------- -------
Index 0 Index 1 Index 2 Index 3
Arrays may contain no defined elements, one, or many.
Arrays in JS may contain elements of mixed types.
Several array methods and properties are built in to JS to work with arrays.
*/
// ============================= Declaring/Initializing an Array with Array Literal Syntax
// empty arrays may be declared
// var pies = [];
// console.log(pies);
// var pies = ["apple", "cherry", "key lime", "huckleberry"];
// console.log(pies);
// declaring and initializing a large array
// var students = [
// "Student 1",
// "Student 2",
// "Student 3",
// "Student 4",
// "Student 5",
// "Student 6",
// "Student 7",
// "Student 8"
// ];
// !! What index is student 1 at? Student 7? Student 8? !!
// separate declaration and initialization
// var pies = [];
// pies[0] = "apple";
// pies[1] = "cherry";
// declaring and initializing a high index number
// pies[10] = "huckleberry";
// console.log(pies);
// ============================= Counting Array Items
// var pies = ["apple", "cherry", "key lime", "huckleberry"];
// var numberOfPies = pies.length;
// console.log(numberOfPies);
// ============================= Accessing Array Elements
// console.log(pies[100]);
// console.log(pies[3]);
// ============================= For Loop
/* SYNTAX
for (var i = 0; i < someArray.length; i += 1) {
console.log(someArray[i]);
}
- in the above context "someArray[i]" will access the next array element in each iteration
- PROS: variety of traversals
- CONS: more complicated syntax
- USE: when you need to iterate partially or backwards through an array
*/
// example
// for (var i = 0; i < pies.length; i += 1) {
// console.log("I like " + pies[i]);
// }
// backwards
// for (var i = pies.length - 1; i >= 0; i -= 1) {
// console.log("I like " + pies[i]);
// }
//
// console.log(pies);
// !! How could we print out every other pie from the beginning of the array? !!
// ============================= Foreach Loop
/* SYNTAX
someArray.forEach(function(element, index, array){
console.log(element);
console.log(index);
});
- PROS: simpler syntax
- CONS: can only increment through entire array
- USE: when you need to iterate through entire array
REMINDERS...
- the parameters are optional to pass in to the anonymous function
- parameters may be semantically named but order matters
WORKS KINDA LIKE THIS:
var theArray = [some elements...];
function forEach(callback) {
for (var i = 0; i < elements.length; i += 1) {
callback(element, i, theArray);
}
}
*/
// standard syntax
// var pies = ["apple", "cherry", "key lime", "huckleberry"];
// defining all three parameters
// pies.forEach(function(element, index, array){
// console.log("Element " + element + " is at index " + index);
// console.log(pies);
// console.log(array);
// });
// with different parameter names
// pies.forEach(function(pie, index, array){
// console.g(arraylog("Element " + pie + " is at index " + index);
// console.log(pies);
// console.lo);
// });
// with only one parameter
// pies.forEach(function(element){
// console.log("Element " + element);
// });
//
//
// console.log("----------------");
// callback function syntax
// function logElements(element) {
// console.log("Element " + element);
// }
//
// pies.forEach(logElements);
// ============================= ARRAYS AND FUNCTIONS
// !! Write a function, logNums, that takes in a array and logs each number in the array !!
// var numbers = [7, 5, 2, 4,]
// function logNums(numbers, element) {
// numbers.forEach(function () {
// console.log(element)
// });
// }
// logNums(numbers)
// // numbers.forEach(function (number) {
//
// // })
// }
// logNums(numbers);
// // !! Write a function, returnLongString, that takes in an array of strings and returns all strings concatenated together !!
// function returnLongString(strings){
// // starts w an empty string
// var bigString = '';
// // strings.forEach(function(string) {
// // loop through all string elements and concatenate to string
// // for (var i = 0; i < strings.length; i += 1)
//
// bigString += strings;
// // console.log(strings[i])
// // return string variable
// return bigString;
// }
//
// var strings = [1, 2, 3, 4]
// console.log(returnLongString(strings));
// !! Write a function, returnArrSum, that takes in an array of values and returns the sum of all number elements !!
// function returnArrSum(values){
// // create an output variable
// var output = 0;
// // loop through the values and add the numbers
// values.forEach(function(value){
// // only add the value if it is a number
// // only add values if values is type of 'number'
// if(typeof value === 'number'){
// output += value;}
// });
// return output}
// var values = [1, null, 'hello, 5'];
// console.log(returnArrSum(values));
// !! Write a function, checkIfNumInArray, that takes in an array of numbers and a number and returns the string
// 'input num in input array' if the second argument is an element in the first argument array
// otherwise, the function should return the string 'input num NOT in input array'
// // array to test
// function checkIfNumInArray()
}
// !!
</script>
</body>
</html>