-
Notifications
You must be signed in to change notification settings - Fork 0
/
customFunctions.js
735 lines (607 loc) · 23.5 KB
/
customFunctions.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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
let array = [1, 2, 3, 4];
/**
* @function
* myEach: executes a provided function once for each array element
*
* @param {Array} arr Array to perform the operation on
* @param {Function} callback Function to execute on each element
*
*/
function myEach(arr, callback)
{
for(let i = 0; i < arr.length; i++)
{
// Callback can have 3 parameters:
// - currentValue: The current element being processed in the array
// - index (optional): The index of currentValue in the array
// - array (optional): The array myEach() was called upon
callback(arr[i], i, arr);
}
};
console.log("Default forEach");
array.forEach((x, index, arr) =>
{
console.log(x + arr.length + index);
});
console.log("\nCustom forEach");
myEach(array, (x, index, arr) =>
{
console.log(x + arr.length + index);
});
console.log("\n\n");
/**
* @function
* myMap: creates a new array populated with the results of calling a provided function on every element
* in the calling array
*
* @param {Array} arr Array to perform the operation on
* @param {Function} callback Function to execute on each element
*
* @returns {Array} A new array with each element being the result of the callback function
*/
function myMap(arr, callback)
{
let newArr = [];
arr.forEach((item, index, arr) =>
{
// Callback can have 3 parameters:
// - currentValue: The current element being processed in the array
// - index (optional): The index of currentValue in the array
// - array (optional): The array myMap() was called upon
newArr.push(callback(item, index, arr));
});
return newArr;
};
console.log("Default map");
console.log(array.map((x, index, arr) => x + arr.length + index));
console.log("\nCustom map");
console.log(myMap(array, (x, index, arr) => x + arr.length + index));
console.log("\n\n");
/**
* @function
* myFilter: creates a new array with all elements that pass the test implemented by the provided function
*
* @param {Array} arr Array to perform the operation on
* @param {Function} callback Function to test each element of the array.
* Return true to keep the element, false otherwise
*
* @returns {Array} A new array with the elements that pass the past. If no elements pass the test,
* an empty array will be returned
*/
function myFilter(arr, callback)
{
let newArr = [];
arr.forEach((item, index, arr) =>
{
// Callback can have 3 parameters:
// - element: The current element being processed in the array
// - index (optional): The index of the current element being processed in the array
// - array (optional): The array myFilter() was called upon
callback(item, index, arr) ? newArr.push(item) : newArr;
});
return newArr;
}
console.log("Default filter");
console.log(array.filter((x, index, arr) => x % 2 === 0));
console.log("\nCustom filter");
console.log(myFilter(array, (x, index, arr) => x % 2 === 0));
console.log("\n\n");
/**
* @function
* mySome: tests whether at least one element in the array passes the test implemented by the provided Function
*
* @param {Array} arr Array to perform the operation on
* @param {Function} callback Function to test for each element
*
* @returns {boolean} true if the callback function returns a truthy value for at least one element in the array.
* Otherwise, false
*/
function mySome(arr, callback)
{
let hasSome = false;
arr.forEach((item, index, arr) =>
{
// Callback can have 3 parameters:
// - element: The current element being processed in the array
// - index (optional): The index of the current element being processed in the array
// - array (optional): The array mySome() was called upon
if(callback(item, index, arr)) hasSome = true;
});
return hasSome;
}
console.log("Default some");
console.log("Is there a 2: " + array.some(x => x === 2));
console.log("\nCustom some");
console.log("Is there a 2: " + mySome(array, x => x === 2));
console.log("\n\n");
/**
* @function
* myEvery: tests whether all elements in the array pass the test implemented by the provided function
*
* @param {Array} arr Array to perform the operation on
* @param {Function} callback Function to test for each element
*
* @returns {boolean} true if the callback function returns a truthy value for every array element. Otherwise, false
*/
function myEvery(arr, callback)
{
let hasEvery = true;
arr.forEach((item, index, arr) =>
{
// Callback can have 3 parameters:
// - element: The current element being processed in the array
// - index (optional): The index of the current element being processed in the array
// - array (optional): The array myEvery() was called upon
if(!callback(item, index, arr)) hasEvery = false;
});
return hasEvery;
}
console.log("Default every");
console.log("Are all numbers less than 5: " + array.every(x => x < 5));
console.log("Are all numbers greater than 3: " + array.every(x => x > 3));
console.log("\nCustom every");
console.log("Are all numbers less than 5: " + myEvery(array, x => x < 5));
console.log("Are all numbers greater than 3: " + myEvery(array, x => x > 3));
console.log("\n\n");
/**
* @function
* myReduce: executes a reducer function (that you provide) on each element of the array,
* resulting in a single output value
*
* @param {Array} arr Array to perform the operation on
* @param {Function} callback Function to test for each element
* @param {Number} initialValue (Optional) Initial value to start accumulating from
*
* @returns {Number} the single value that results from the reduction
*/
function myReduce(arr, callback, initialValue)
{
if(arr.length === 0) return initialValue;
let accumulator;
arr.forEach((item, index, arr) =>
{
if(index === 0) (initialValue !== undefined || null) ? accumulator = initialValue : accumulator = 0;
// Callback can have 4 parameters:
// - accumulator: The accumulated return values of callback. It is the accumulated value previously returned
// in the last invocation of the callback, or initialValue, if it was supplied
// - currentValue: The current element being processed in the array
// - index (optional): The index of currentValue being processed in the array
// - array (optional): The array myReduce() was called upon
accumulator = callback(accumulator, item, index, arr);
});
return accumulator;
}
console.log("Default reduce");
console.log("Sum of all values in array: " + array.reduce((count, current) =>
{
count += current;
return count;
}));
console.log("\nCustom reduce");
console.log("Sum of all values in array: " + myReduce(array, (count, current) =>
{
count += current;
return count;
}));
console.log("\n\n");
/**
* @function
* myIncludes: determines whether an array includes a certain value among its entries,
* returning true or false as appropriate
*
* @param {Array} arr Array to perform the operation on
* @param {Object} valueToFind The value to search for
* @param {Number} fromIndex (Optional) The position in the array which to begin searching for valueToFind.
* Defaults to 0
*
* @returns {Boolean} which is true if the value valueToFind is found within the array
* (or the part of the array indicated by the index fromIndex, if specified)
*/
function myIncludes(arr, valueToFind, fromIndex)
{
let startingIndex = fromIndex === undefined ? 0 : fromIndex;
for(let i = startingIndex; i < arr.length; i++)
{
if(valueToFind === arr[i]) return true;
}
return false;
}
console.log("Default includes");
console.log("Does the array include 3: " + array.includes(3));
console.log("\nCustom includes")
console.log("Does the array include 3: " + myIncludes(array, 3));
console.log("\n\n");
/**
* @function
* myIndexOf: returns the first index at which a given element can be found in the array, or -1 if it is not present
*
* @param {Array} arr Array to perform the operation on
* @param {Object} searchElement Element to locate in the array
* @param {Number} fromIndex (Optional) The index to start the search at. If the index is greater than or equal to
* the array's length, -1 is returned, which means the array will not be searched
*
* @returns {Number} the first index of the element in the array, -1 if not found
*/
function myIndexOf(arr, searchElement, fromIndex)
{
let startingIndex = fromIndex === undefined ? 0 : fromIndex;
for(let i = startingIndex; i < arr.length; i++)
{
if(searchElement === arr[i]) return i;
}
return -1;
}
console.log("Default indexOf");
console.log("Index of 4: " + array.indexOf(4));
console.log("Index of 5: " + array.indexOf(5));
console.log("\nCustom indexOf");
console.log("Index of 4: " + myIndexOf(array, 4));
console.log("Index of 5: " + myIndexOf(array, 5));
console.log("\n\n");
/**
* @function
* myPush: adds one or more elements to the end of an array and returns the new length of the array
*
* @param {Array} arr Array to perform the operation on
* @param {...Object} elements The element(s) to add to the end of the array
*
* @returns {Number} The new length of the array
*/
function myPush(arr, ...elements)
{
arr = new Array(...arr, ...elements);
return arr.length;
}
console.log("Default push");
console.log("Before pushing: " + array);
console.log("Length of new array: " + array.push(5, 6, 7, 8, 9));
array = [1, 2, 3, 4]
console.log("\nCustom push");
console.log("Before pushing: " + array);
console.log("Length of new array: " + myPush(array, 5, 6, 7, 8, 9));
console.log("\n\n");
/**
* @function
* myLastIndexOf: returns the last index at which a given elment can be found in the array, or -1 if it is not present.
* The array is searched backwards, starting at fromIndex
*
* @param {Array} arr Array to perform the operation on
* @param {Object} searchElement Element to locate in the array
* @param {Number} fromIndex (Optional) The index at which to start searching backwards. Defaults to (arr.length - 1).
* If the index is greater than or equal to the length of the array, the whole array will be searched
*
* @returns {Number} the last index of the element in the array, -1 if not found
*/
function myLastIndexOf(arr, searchElement, fromIndex)
{
let startingIndex = fromIndex === undefined ? arr.length - 1 : fromIndex;
for(let i = startingIndex; i >= 0; i--)
{
if(searchElement === arr[i]) return i;
}
return -1;
}
let array2 = [2, 4, 6, 4, 2];
console.log("Default lastIndexOf");
console.log("Last index of 4: " + array2.lastIndexOf(4));
console.log("Last index of 4 starting from index 2: " + array2.lastIndexOf(4, 2));
console.log("\nCustom lastIndexOf");
console.log("Last index of 4: " + myLastIndexOf(array2, 4));
console.log("Last index of 4 starting from index 2: " + myLastIndexOf(array2, 4, 2));
console.log("\n\n");
/**
* @function
* grabKeys: returns an array of a given object's own enumerable property 'names', iterated in the
* same order that a normal loop would
*
* @param {Object} obj The object of which the enumerable's own properties are to be returned
*
* @returns {Array} An array of strings that represent all the enumerable properties of the given object
*/
Object.prototype.grabKeys = function(obj)
{
let keys = [];
for(const key in obj)
{
// Make sure obj is actually an object and not null
// Check if the object itself has the keys (if array does in fact contain the elements)
// This prevents the grabKeys function name to be added to the array
// since it is not a property of the obj
// Another way of writing this is
// obj.hasOwnProperty(key)
if(Object.prototype.hasOwnProperty.call(obj, key)) keys.push(key);
}
return keys;
};
let arrObj = ['a', 'b', 'c'];
let obj = { 0: 'a', 1: 'b', 2: 'c'};
let randObj = { 100: 'a', 2: 'b', 7: 'c'};
console.log("Default keys");
console.log("arrObj keys: " + Object.keys(arrObj));
console.log("obj keys: " + Object.keys(obj));
console.log("randObj keys: " + Object.keys(randObj));
console.log("\nCustom keys");
console.log("arrObj keys: " + Object.grabKeys(arrObj));
console.log("obj keys: " + Object.grabKeys(obj));
console.log("randObj keys: " + Object.grabKeys(randObj));
console.log("\n\n");
/**
* @function
* grabValues: returns an array of a given object's own enumerable property values, in the same order as that is
* provided by a for...in loop
*
* @param {Object} obj The object of which the enumerable's own properties are to be returned
*
* @returns {Array} An array of containing the given object's own enumerable property values
*/
Object.prototype.grabValues = function(obj)
{
let values = [];
for(const key in obj)
{
// Make sure obj is actually an object and not null
// Check if the object itself has the keys (if array does in fact contain the elements)
// This prevents the grabValues function to be added to the array
// since it is not a property of the obj
// Another way of writing this is
// obj.hasOwnProperty(key)
if(Object.prototype.hasOwnProperty.call(obj, key)) values.push(obj[key]);
}
return values;
}
console.log("Default values");
console.log("arrObj values: " + Object.values(arrObj));
console.log("obj values: " + Object.values(obj));
console.log("randObj values: " + Object.values(randObj));
console.log("\nCustom values");
console.log("arrObj values: " + Object.grabValues(arrObj));
console.log("obj values: " + Object.grabValues(obj));
console.log("randObj values: " + Object.grabValues(randObj));
console.log("\n\n");
/*
==============================================================================
Miscellaneous Problems: EJS Chapter 4 - Data Structures (Objects and Arrays)
==============================================================================
*/
/*
The Sum of a Range
Write a range function that takes two arguments, start and end, and
returns an array containing all the numbers from start up to (and including) end.
Next, write a sum function that takes an array of numbers and returns the sum of these numbers.
As a bonus, modify the range function to take an optional third argument that indicates the
"step" value used when building the array.
If no step is given, the elements go up by increments of one, corresponding to the old behavior.
The function call range(1, 10, 2) should return [1, 3, 5, 7, 9]
Make sure it also works with negative step values so that the range(5, 2, -1) produces [5, 4, 3, 2]
*/
/**
* @function
* range: returns an array containing all the numbers from start up to (and including) end
*
* @param {Number} start The number to start from
* @param {Number} end The number to end from
* @param {Number} step The increment amount between start and end
*
* @returns {Array} The array containing all the numbers from start up to (and including) end
*/
function range(start, end, step)
{
let arr = [];
if(start < end)
{
for(let i = start; i <= end; (typeof step == "number" && step > 0) ? i += step : i++)
{
arr.push(i);
}
}
else if(start > end)
{
for(let i = start; i >= end; (typeof step == "number" && step < 0) ? i += step : i--)
{
arr.push(i);
}
}
else if(start === end) arr = [start, end];
return arr;
}
/**
* @function
* sum: returns the sum of all elements within an array
*
* @param {Array} arr The array of numbers to be summed
*
* @returns {Number} The sum of all the elements of the provided array
*/
function sum(arr)
{
return arr.reduce((sum, item) =>
{
sum += item;
return sum;
}, 0);
}
console.log("Sum of numbers from 1 to 10: " + sum(range(1, 10)));
console.log("Sum of numbers from 3 to 9: " + sum(range(3, 9)));
console.log("Sum of numbers from 10 to 1: " + sum(range(10, 1)));
console.log("Sum of numbers from 9 to 3: " + sum(range(9, 3)));
console.log("\nSum of numbers from 1 to 10 step 2: " + sum(range(1, 10, 2)));
console.log("Sum of numbers from 3 to 9 step 2: " + sum(range(3, 9, 2)));
console.log("Sum of numbers from 10 to 1 step -2: " + sum(range(10, 1, -2)));
console.log("Sum of numbers from 9 to 3 step -2: " + sum(range(9, 3, -2)));
console.log("\nSum of numbers from 1 to 10 step -2: " + sum(range(1, 10, -2)));
console.log("Sum of numbers from 10 to 1 step 2: " + sum(range(10, 1, 2)));
console.log("\nSum of numbers from 5 to 5: " + sum(range(5, 5)));
console.log("Sum of numbers from 5 to 5 step 2: " + sum(range(5, 5, 2)));
console.log("Sum of numbers from 5 to 5 step -2: " + sum(range(5, 5, -2)));
console.log("\n\n");
/*
Reversing an Array
reverseArray: takes an array as an argument and produces a new array that has
the same elements in the inverse order
reverseArrayInPlace: does the same as reverseArray, but modifies the array given
as an argument by reversing its elements
Neither functions may use the vanilla reverse function
*/
/**
* @function
* reverseArray: takes an array as an argument and produces a new array that has
* the same elements in the reverse order
*
* @param {Array} arr The array to reverse
*
* @returns {Array} The reversed array
*/
function reverseArray(arr)
{
let newArr = [];
for(let i = arr.length - 1; i >= 0; i--)
{
newArr.push(arr[i]);
}
return newArr;
}
/**
* @function
* reverseArrayInPlace: does the same as reverseArray, but modifies the array given
* as an argument by reversing its elements
*
* @param {Array} arr The array to reverse in place
*
* @returns {Array} The reversed array
*/
function reverseArrayInPlace(arr)
{
for(let i = 0; i < arr.length / 2; i++)
{
let temp = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = arr[i];
arr[i] = temp;
}
return arr;
}
let reverseThis = [1, 2, 3, 4, 5]
let anotherReverse = [4, 7, 9, 10]
console.log("Reverse: " + reverseArray(reverseThis));
console.log("Reverse in place: " + reverseArrayInPlace(reverseThis));
console.log("\nReverse: " + reverseArray(anotherReverse));
console.log("Reverse in place: " + reverseArrayInPlace(anotherReverse));
console.log("\n\n");
/*
A List
A list (not to be confused with array), is a nested set of objects, with the
first object holding a reference to the second, the second to the third, and so on.
Write a function arrayToList that builds up a list structure like the one shown when
given [1, 2, 3] as an argument.
arrayToList([10, 20]) -> {value: 10, rest: {value: 20, rest: null}}
Also write a listToArray function that produces an array from a list.
listToArray(arrayToList([10, 20, 30])) -> [10, 20, 30]
Then add a helper function prepend, which takes an element and a list and creates a
new list that adds the element to the front of the input list.
prepend(10, prepend(20, null)) -> {value: 10, rest: {value: 20, rest: null}}
Then add nth, which takes a list and a number and returns the element at the given
position in the list (with zero referring to the first element) or underfined when
there is no such element. Also write a recursive version of nth.
nth(arrayToList([10, 20, 30]), 1) -> 20
*/
function arrayToList(arr)
{
if(arr === null || arr === undefined) return null;
if(arr.length === 0) return {};
let list = null;
for(let i = arr.length - 1; i >= 0; i--)
{
list = prepend(arr[i], list);
}
return list;
}
console.log("[10, 20] to list: ");
console.log(arrayToList([10, 20]));
function listToArray(list)
{
let array = [];
while(list !== null)
{
array.push(nth(list, 0));
list = list.rest;
}
return array;
}
console.log("\nList back to array: ");
console.log(listToArray(arrayToList([10, 20])));
function prepend(element, list)
{
return {value: element, rest: list};
}
console.log("\nPrepend values to create list: ");
console.log(prepend(10, prepend(20, null)));
function nth(list, index)
{
if(index === 0) return list.value;
return nth(list.rest, index - 1);
}
console.log("\nThe value at index 1 is: ");
console.log(nth(arrayToList([10, 20, 30]), 1));
console.log("\n\n");
/*
Deep Comparison
Write a function deepEqual that takes two values and returns true only if
they are the same value or are objects with the same properties, where the
values of the properties are equal when compared with a recursive call to
deepEqual.
To find out whether values should be compared directly (=== operator) or
have their properties compared, you can use the typeof operator. If it produces
"object" for both values, you should do a deep comparison. But you have to
take into account that typeof null also produces "object".
console.log(deepEqual(deepObj, deepObj)) -> true
console.log(deepEqual(deepObj, {here: 1, object: 2})) -> false
console.log(deepEqual(deepObj, {here: {is: "an"}, object: 2})) -> true
*/
function deepEqual(obj1, obj2)
{
if(obj1 === obj2) return true;
if((typeof obj1 != "object" || obj1 == null) || (typeof obj2 != "object" || obj2 == null)) return false;
let keys1 = Object.keys(obj1);
let keys2 = Object.keys(obj2);
// Checking if objects have the same number of properties
if(keys1.length != keys2.length) return false;
// Go through each property of the first object
// For some reason for...in causes the key to return 0 value
// But for...of works, probably because we have an array of keys
for(const key of keys1)
{
// Check if second object has the same, and then do recursion to go deeper in the list
if(!keys2.includes(key) || !deepEqual(obj1[key], obj2[key])) return false;
}
return true;
}
let deepObj = {here: {is: "an"}, object: 2};
console.log(deepEqual(deepObj, deepObj));
console.log(deepEqual(deepObj, {here: 1, object: 2}));
console.log(deepEqual(deepObj, {here: {is: "an"}, object: 2}));
console.log("\n\n");
/*
Given an array nums, write a function to move all 0's to the end of it while
maintaining the relative order of the non-zero elements
Input: [0, 1, 0, 3, 12]
Output: [1, 3, 12, 0, 0]
Note: You must do this in place without making a copy of the array
*/
function moveZeros(nums)
{
// Number of non-zero elements
let count = 0;
for(let i = 0; i < nums.length; i++)
{
// For each non-zero element, start from index 0 and place them
// in order while incrementing count.
// This will maintain the ordering while keeping track of which
// elements are 0 and which are not
if(nums[i] != 0) nums[count++] = nums[i];
}
// Now that we know how many non-zero elements there are and the length
// of the array, we can simply fill in the rest of the array with 0
while(count < nums.length) nums[count++] = 0;
return nums;
}
console.log("Move zeros: ");
console.log(moveZeros([0, 1, 0, 3, 12]));