-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathES6javascripthelpers.js
326 lines (232 loc) · 6.64 KB
/
ES6javascripthelpers.js
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//for of loop
const examplecolors = ['orange', 'purple', 'yellow'];
for (let examplecolor of examplecolors) {
console.log(examplecolor);
}
const examplenumbers = [1, 2, 3, 4];
let exampleTotal = 0;
for (let examplenumber of examplenumber) {
exampleTotal += number;
}
console.log(exampleTotal);
//Moving on
var colors = [ 'red', 'blue', 'green' ];
//the traditional forLoop goes this way
for (var i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
//say hello to better JS array helper methods
//The forEach Helper
colors.forEach(function(color) {
console.log(color);
});
var numbers = [1,2,3,4,5];
var sum = 0;
numbers.forEach(function(number) {
sum += number;
});
console.log(sum);
//The inner function of the forEach don't need to be anonymous.
//We can define it separately and then pass it into the forEach
function adder(number) {
sum += number;
}
numbers.forEach(adder);
//The Map Helper
var numbers = [1,2,3];
var doubledNumbers = [];
//traditionally we can go
for (var i = 0; i < numbers.length; i++) {
doubledNumbers.push(numbers[i] * 2);
}
//using map (always put the return keyword in map statements)
var doubled = numbers.map(function(number) {
return number * 2;
});
//the below operation is what is known as a pluck
//because we are plucking a property off an object
var cars = [
{model: 'Buick', price: 'CHEAP'},
{model: 'Camaro', price: 'EXPENSIVE'}
];
var prices = cars.map(function(car) {
return car.price;
});
console.log(prices);
//The Filter Helper
var products = [
{ name: 'cucumber', type: 'vegetable', quantity: 0, price: 1 },
{ name: 'banana', type: 'fruit', quantity: 10, price: 15 },
{ name: 'celery', type: 'vegetable', quantity: 30, price: 9 },
{ name: 'orange', type: 'fruit', quantity: 3, price: 5 }
];
var filteredProducts = [];
for (var i = 0; i < products.length; i++) {
if (products[i].type === 'fruit') {
filteredProducts.push(products[i]);
}
}
console.log(filteredProducts);
//let's refactor the above
products.filter(function(product) {
return product.type === 'fruit';
});
//more
//Type is 'vegetable', quantity > 0, price < 10
products.filter(function(product) {
return product.type === 'vegetable'
&& product.quantity > 0
&& product.price < 10
});
var post = { id: 4, title: 'New Post' };
var comments = [
{ postId: 4, content: 'awesome post' },
{ postId: 3, content: 'it was ok' },
{ postId: 4, content: 'neat' }
];
function commentsForPost(post, comments) {
return comments.filter(function(comment) {
return comment.postId === post.id;
});
}
commentsForPost(post, comments);
//The Find Helper
//Very flexible. Whenever you have a single record
//or you're trying to find it inside a larger collection
//and you want just one singular element out of there
var users = [
{ name: 'Jill'},
{ name: 'Alex'},
{ name: 'Bill'}
];
var desiredUser = '';
for (var i = 0; i < users.length; i++) {
if (users[i].name === 'Alex') {
desiredUser = users[i];
break;
}
}
console.log(user);
// refactor
users.find(function(user) { //this helper exits once it finds a match
return user.name === 'Alex';
});
//more
function Car(model) {
this.model = model;
}
var cars = [
new Car('Buick'),
new Car('Camaro'),
new Car('Focus')
];
cars.find(function(car) {
return car.model === 'Focus';
});
var posts = [
{ id: 1, title: 'New Post' },
{ id: 2, title: 'Old Post' }
];
var comment = { postId: 1, content: 'Great Post' };
function postForComment(posts, comment) {
return posts.find(function(post) {
return post.id === comment.postId;
});
}
postForComment(posts, comment);
//'every' helper and 'some' helper
var computers = [
{ name: 'Apple', ram: 24 },
{name: 'Compaq', ram: 4 },
{ name: 'Acer', ram: 32 }
];
var allComputersCanRunProgram = true;
var onlySomeComputersCanRunProgram = false;
for (var i = 0; i < computers.length; i++) {
var computer = computers[i];
if (computer.ram < 16) {
allComputersCanRunProgram = false;
} else {
onlySomeComputersCanRunProgram = true;
}
}
computers.every(function(computer) {
return allComputersCanRunProgram = computer.ram > 8; //this returns false because not every computer is greater than 16
});
computers.some(function(computer) {
return onlySomeComputersCanRunProgram = computer.ram > 16;
});
console.log(allComputersCanRunProgram);
console.log(onlySomeComputersCanRunProgram);
var names = [
"Alexandria",
"Matthew",
"Joe"
];
var allNames = true
var someNames = false;
names.every(function(name) {
return allNames = name.length > 4;
});
names.some(function(name) {
return someNames = name.length > 4;
});
console.log(allNames);
console.log(someNames);
function Field(value) {
this.value = value;
}
//let me add a method to Field
Field.prototype.validate = function() {
return this.value.length > 0; //we are saying a field is valid if it has a length greater than zero
}
var username = new Field("2cool");
var password = new Field("my_password");
var birthdate = new Field("10/10/2010");
console.log(username.validate() && password.validate());
//Instead of listing out all the fields the console.log statement, we can instead
var fields = [username, password, birthdate];
var formIsValid = fields.every(function(field) {
return field.validate();
});
if (formIsValid) {
// allow user to submit!
} else {
//show an error message
}
//The reduce helper
var numbers = [ 10, 20, 30 ];
var sum = 0;
for (var i = 0; i < numbers.length; i++) {
sum += numbers[i]; //60
}
numbers.reduce(function(sum, number) {
return sum + number;
}, 0); //with reduce, we pass in a second argument (initial value of sum)
var primaryColors = [
{ color: 'red' },
{ color: 'yellow' },
{ color: 'blue' }
];
var results = primaryColors.reduce(function(previous, primaryColor) {
previous.push(primaryColor.color);
return previous;
}, []);
console.log(results);
function balancedParens(string) {
turnToarray = string.split(""); //we convert to an array because helpers only work with arrays
return !turnToarray.reduce(function(previous, char) { //JS interpretes a positive or negative number as truthy and interpretes zero as being falsey
if (previous < 0) { //with this code, if the string starts with ')', it is instantly unbalanced no matter if it even out
return previous;
}
if (char === "(") {
return ++previous;
}
if (char === ")") {
return --previous;
}
return previous;
}, 0);
}
console.log(balancedParens("((((")); //opening brackets with no closing partners, so not balanced
console.log(balancedParens(")(")); //unbalanced