Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
455 lines (426 loc) · 13.5 KB
/
Copy pathscript.js
File metadata and controls
455 lines (426 loc) · 13.5 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// Test using EXAMPLE functions given below each function.
let number = [1,3,4,6,7,9,15,17]
/*
Create a function named addTwo which accepts a number, adds 2 to the number and return the new value.
*/
let addTwo = function (number) {
return number + 2;
};
/*
Create a function named addThree which accepts a number, adds 3 to the number and return the new value.
*/
let addThree = function (number) {
return number + 3;
}
/*
Create a function named addFive which accepts a number, adds 5 to the number and return the new value.
*/
let addFive = function (number) {
return number + 5;
}
/*
Create a function named addTwoToArray which accepts:
- An array of numbers
- Returns a new array where 2 is added to each element
- While doing so use the funciton addTwo
*/
function addTwoToArray (arr){
let finalArray = [];
for(let num of arr){
finalArray.push(addTwo(num));
} return finalArray;
}
console.log(addTwoToArray(number));
/*
Create a function named addThreeToArray which accepts:
- an array of numbers
- returns a new array where 2 is added to each element
- while doing so use the funciton addThree
*/
function addThreeToArray (arr){
let finalArray = [];
for(let num of arr){
finalArray.push(addThree(num));
} return finalArray;
}
console.log(addThreeToArray(number));
/*
Create a function named addFiveToArray which accepts:
- an array of numbers
- aeturns a new array where 2 is added to each element
- while doing so use the funciton addThree
*/
function addFiveToArray (arr){
let finalArray = [];
for(let num of arr){
finalArray.push(addFive(num));
} return finalArray;
}
console.log(addFiveToArray(number));
/*
In above function addTwoToArray, addThreeToArray, addFiveToArray we are repeating the code, let's fix this.
Create a function named changeArray which accepts two arguments:
- an array of numbers
- a callback function
- pass each element of array to the callback function
- and store the value returned from callback function in a new array
- return the new array
EXAMPLE:
*/
function changeArray (arr, cb) {
let finalArray = [];
for(let num of arr) {
if(cb(num)) {
finalArray.push(cb(num));
}
} return finalArray;
}
console.log(changeArray([1, 2, 3, 4, 5, 6], addTwo)); // [3, 4, 5, 6, 7, 8]
console.log(changeArray([1, 2, 3, 4, 5, 6], addThree)); // [4, 5, 6, 7, 8, 9]
console.log(changeArray([1, 2, 3, 4, 5, 6], addFive)); // [6, 7, 8, 9, 10, 11]
/*
Create a function called sendMessage that accepts two arguments:
- a string (message) and a function (callback).
- it will return the result of the callback function called with the message as argument
EXAMPLE:
*/
function sendMessage(msg, cb) {
let message = cb(msg);
return message;
}
console.log(sendMessage("Hello", console.log));
console.log(sendMessage("Hello Alert!", alert));
console.log(sendMessage("Hello Prompt!", prompt));
/*
Create a function named `first` that accepts an argument a function (callback) and return the same function defination.
*/
let first = function(cb) {
function cb () {
}
return cb();
}
/*
Create a function named `second`
- Inside second create another function named `third` which accepts a number, adds 1 to it and returns it
- Return the function defination (third) from the second function
- Also write the required code to call the function
*/
function second(){
function third(num) {
return num + 1;
}
return third;
}
console.log(second());
/*
Write a function named `callMe` which
- accept a function (callback function) as argument.
- Store the value returned by calling the callback function in a variable named `final`
- return final varibale from the function.
- also write the required code to call the function.
*/
function callMe (cb) {
let final = cb();
return final;
}
let test = callMe(function(){
return 21;
})
// Data Starts (Don't change this)
const people = [
{ name: 'John Doe', age: 16 },
{ name: 'Thomas Calls', age: 19 },
{ name: 'Liam Smith', age: 20 },
{ name: 'Jessy Pinkman', age: 18 },
];
const grades = [
{ name: 'John', grade: 8, sex: 'M' },
{ name: 'Sarah', grade: 12, sex: 'F' },
{ name: 'Bob', grade: 16, sex: 'M' },
{ name: 'Johnny', grade: 2, sex: 'M' },
{ name: 'Ethan', grade: 4, sex: 'M' },
{ name: 'Paula', grade: 18, sex: 'F' },
{ name: 'Donald', grade: 5, sex: 'M' },
{ name: 'Jennifer', grade: 13, sex: 'F' },
{ name: 'Courtney', grade: 15, sex: 'F' },
{ name: 'Jane', grade: 9, sex: 'F' },
];
// Data Ends
/*
Create a function named isAdult which accepts:
- an object
- returns true or false based on the `age` property in the object is greater than 18 or not
EXAMPLE:
*/
function isAdult(obj){
return obj.age > 18;
}
console.log(isAdult(people[0])); // false
console.log(isAdult(people[1])); // true
console.log(isAdult(people[2])); // true
/*
Create a function named isMale which accepts:
- an object
- returns true or false based on the `sex` property in the object is `M` or not
EXAMPLE:
*/
function isMale(obj){
return obj.sex == "M";
}
console.log(isMale(grades[0])); // true
console.log(isMale(grades[1])); // false
console.log(isMale(grades[2])); // true
/*
Create a function named isFemale which accepts:
- an object
- returns true or false based on the `sex` property in the object is `F` or not
EXAMPLE:
*/
function isFemale(obj){
return obj.sex == "F";
}
console.log(isFemale(grades[0])); // false
console.log(isFemale(grades[1])); // true
console.log(isFemale(grades[2])); // false
/*
Create a function named isGradeA which accepts:
- an object
- returns true or false based on the `grade` property in the object is greater than 12 or not
EXAMPLE:
*/
function isGradeA(obj){
return obj.grade > 12;
}
console.log(isGradeA(grades[0])); // false
console.log(isGradeA(grades[1])); // false
console.log(isGradeA(grades[2])); // true
/*
Create a function named isGradeB which accepts:
- an object
- returns true or false based on the `grade` property in the object is greater than 8 and less than or equal to 12 or not
EXAMPLE:
*/
function isGradeB(obj){
return obj.grade > 8 && obj.grade <=12;
}
console.log(isGradeB(grades[0])); // true
console.log(isGradeB(grades[1])); // true
console.log(isGradeB(grades[2])); // false
/*
Create a function named isGradeC which accepts:
- an object
- returns true or false based on the `grade` property in the object is less than or equal to 8 or not
EXAMPLE:
console.log(isGradeC(grade[0])); // true
console.log(isGradeC(grade[1])); // false
console.log(isGradeC(grade[2])); // false
*/
function isGradeC(obj) {
return obj.grade <= 8;
}
console.log(isGradeC(grades[0])); // true
console.log(isGradeC(grades[1])); // false
console.log(isGradeC(grades[2])); // false
/*
Create a function named filterAdult which accepts:
- an array of objects
- returns a new array with the value of age greater than 18 or not
- while doing so use the funciton isAdult you created earlier
EXAMPLE:
console.log(filterAdult(people));
Output:
[
{ name: 'Thomas Calls', age: 19 },
{ name: 'Liam Smith', age: 20 },
];
*/
function filterAdult (arr){
let finalArray = [];
for( let person of arr){
if(isAdult(person)) {
finalArray.push(person);
}
}
return finalArray;
}
console.log(filterAdult(people));
/*
Create a function named filterMale which accepts:
- an array of objects
- returns a new array with the value of sex is `M` or not
- while doing so use the funciton isMale you created earlier
EXAMPLE:
Output:
[
{ name: 'John', grade: 8, sex: 'M' },
{ name: 'Bob', grade: 16, sex: 'M' },
{ name: 'Johnny', grade: 2, sex: 'M' },
{ name: 'Ethan', grade: 4, sex: 'M' },
{ name: 'Donald', grade: 5, sex: 'M' },
];
*/
function filterMale (arr){
let finalArray = [];
for( let person of arr){
if(isMale(person)) {
finalArray.push(person);
}
}
return finalArray;
}
console.log(filterMale(grades));
/*
Create a function named filterFemale which accepts:
- an array of objects
- returns a new array with the value of sex is `F` or not
- while doing so use the funciton isFemale you created earlier
EXAMPLE:
console.log(filterFemale(grade));
Output:
[
{ name: 'Sarah', grade: 12, sex: 'F' },
{ name: 'Paula', grade: 18, sex: 'F' },
{ name: 'Jennifer', grade: 13, sex: 'F' },
{ name: 'Courtney', grade: 15, sex: 'F' },
{ name: 'Jane', grade: 9, sex: 'F' },
]
*/
function filterFemale (arr){
let finalArray = [];
for( let person of arr){
if(isFemale(person)) {
finalArray.push(person);
}
}
return finalArray;
}
console.log(filterFemale(grades));
/*
Create a function named filterGradeA which accepts:
- an array of objects
- returns a new array with the value of grade is greater than 12 or not
- while doing so use the funciton isGradeA you created earlier
EXAMPLE:
console.log(filterGradeA(grade));
Output:
[
{ name: 'Sarah', grade: 12, sex: 'F' },
{ name: 'Paula', grade: 18, sex: 'F' },
{ name: 'Jennifer', grade: 13, sex: 'F' },
{ name: 'Courtney', grade: 15, sex: 'F' },
{ name: 'Jane', grade: 9, sex: 'F' },
]
*/
function filterGradeA (arr){
let finalArray = [];
for( let person of arr){
if(isGradeA(person)) {
finalArray.push(person);
}
}
return finalArray;
}
console.log(filterGradeA(grades));
/*
Create a function named filterGradeB which accepts:
- an array of objects
- returns a new array with the value of grade is greater than 8 and less than or equal to 12 or not
- while doing so use the funciton isGradeB you created earlier
EXAMPLE:
console.log(filterGradeB(grade));
Output:
[
{ name: 'Sarah', grade: 12, sex: 'F' },
{ name: 'Johnny', grade: 2, sex: 'M' },
{ name: 'Ethan', grade: 4, sex: 'M' },
{ name: 'Donald', grade: 5, sex: 'M' },
{ name: 'Jane', grade: 9, sex: 'F' },
]
*/
function filterGradeB (arr){
let finalArray = [];
for( let person of arr){
if(isGradeB(person)) {
finalArray.push(person);
}
}
return finalArray;
}
console.log(filterGradeB(grades));
/*
Create a function named filterGradeC which accepts:
- an array of objects
- returns a new array with the value of grade is greater less or equal to 8 or not
- while doing so use the funciton isGradeC you created earlier
EXAMPLE:
console.log(filterGradeC(grade));
Output:
[
{ name: 'John', grade: 8, sex: 'M' },
{ name: 'Johnny', grade: 2, sex: 'M' },
{ name: 'Ethan', grade: 4, sex: 'M' },
{ name: 'Donald', grade: 5, sex: 'M' },
]
*/
function filterGradeC (arr){
let finalArray = [];
for( let person of arr){
if(isGradeC(person)) {
finalArray.push(person);
}
}
return finalArray;
}
console.log(filterGradeC(grades));
/*
We are repeating lots of code in above functions like filterGradeC, filterGradeB, filterGradeA, filterAdult. We will fix
this by making a higher order function named filter. Now using one function filter we will be able to filter anything we want.
filter is a higher order function.
Create a function named filter which accepts:
- an array of objects
- a callback function (which should return true or false)
- call the callback function with each object of array
- if callback function returns true add to new array otherwise don't
- returns a new array
EXAMPLE:
console.log(filter(people, isAdult));
console.log(filter(grade, isMale));
console.log(filter(grade, isFemale));
console.log(filter(grade, isGradeA));
console.log(filter(grade, isGradeB));
console.log(filter(grade, isGradeC));
*/
function filter (arr, cb) {
let finalArray = [];
for(let person of arr) {
if(cb(person)) {
finalArray.push(person);
}
} return finalArray;
}
console.log(filter(people, isAdult));
console.log(filter(grades, isMale));
console.log(filter(grades, isFemale));
console.log(filter(grades, isGradeA));
console.log(filter(grades, isGradeB));
console.log(filter(grades, isGradeC));
/*
Create a function named multiplyBy which accepts:
- a number (num)
- and returns a function
- returned function accepts another number (num2) and return num * num2
EXAMPLE:
*/
function multiplyBy(num){
return function(num2) {
return num * num2;
}
}
let multiplyByFive = multiplyBy(5);
console.log(multiplyByFive(10)); // 50
console.log(multiplyByFive(20)); // 100
console.log(multiplyByFive(5)); // 25
let multiplyByNine = multiplyBy(9);
console.log(multiplyByNine(10)); // 90
console.log(multiplyByNine(20)); // 180
console.log(multiplyByNine(5)); // 45