Skip to content

Commit dc583cd

Browse files
authored
New question addition Q11
1 parent 8097aa0 commit dc583cd

File tree

1 file changed

+86
-59
lines changed

1 file changed

+86
-59
lines changed

challenges/collections.md

Lines changed: 86 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,37 @@
1414
8. [Write a program to replace 3 center elements of the 1st array by center 3 elements of the 2nd array](#Q8)
1515
9. [Show how an array in JavaScript can act like a stack and queue](#Q9)
1616
10. [Sort the given array of integers in ascending or descending order](#Q10)
17-
11. [Square all the positive numbers of the array and return the output array](#Q11)
18-
12. [Write a code to generate an array with range of numbers and shuffle them](#Q12)
19-
13. [Check if the user with the name "John" exists in the array of objects](#Q13)
20-
14. [Generate an array of objects with properties id and full name from an array of objects where each object will have id, firstname and lastname](#Q14)
21-
15. [Create an array by removing all the holes of the array](#Q15)
22-
16. [Write a program to calculate the sum of all the values of an array](#Q16)
23-
17. [Get the maximum value from a numbers array along with its index](#Q17)
24-
18. [Find the number of occurences of minimum value in the numbers list](#Q18)
25-
19. [Create an array of length n with all the values of it set to 10](#Q19)
26-
20. [Optimize the given statements having lot of logical checks to use a compact and cleaner logic](#Q20)
27-
21. [Write a program to iterate over a 2 dimensional array and print all the values of it](#Q21)
28-
22. [Write a program to store values in to a set](#Q22)
29-
23. [Write a program to store values in to a map](#Q23)
30-
24. [Write a code to iterate over a set](#Q24)
31-
25. [Write a code to iterate over a map](#Q25)
32-
26. [Show how map is different from object to store key value pairs with coding example](#Q26)
33-
27. [Write the code to remove the duplicates from the array](#Q27)
34-
28. [Design a flat function which flattens an array to any depth](#Q28)
35-
29. [Check if all the students of have passed or not (40 is the pass marks)](#Q29)
36-
30. [Get the average of all the salaries which is greater than 10000 from the department of "IT" from the array of objects)](#Q30)
37-
31. [Extract the list of all the elements from the list of numbers given in 2 arrays](#Q31)
38-
32. [Get the list of all distinct elements which are present in both list of numbers](#Q32)
39-
33. [Extract list of elements present only in the first list given.](#Q33)
40-
34. [Create a function named "average" which can calculate the average of an array and should be available to be called from any Array object.](#Q34)
41-
35. [Write a program to polyfill `filter` functionality of the Array](#Q35)
42-
36. [Write a program to polyfill `map` functionality of the Array](#Q36)
43-
37. [Write a program to polyfill `reduce` functionality of the Array](#Q37)
44-
38. [Write a code to eliminate duplicate objects in an array where each object has an 'id' property which can be used to identify the object and the duplicate object with lower rank to be removed](#Q38)
45-
39. [Create an array which will only accept string values. (Homogeneous array of strings)](#Q39)
46-
40. [Create a Proxy object through which the array can be accessed as usual but also allow to access the values through negative indices](#Q40)
17+
11. [Sort the given array of objects in ascending order according the authors lastname](#Q11)
18+
12. [Square all the positive numbers of the array and return the output array](#Q12)
19+
13. [Write a code to generate an array with range of numbers and shuffle them](#Q13)
20+
14. [Check if the user with the name "John" exists in the array of objects](#Q14)
21+
15. [Generate an array of objects with properties id and full name from an array of objects where each object will have id, firstname and lastname](#Q15)
22+
16. [Create an array by removing all the holes of the array](#Q16)
23+
17. [Write a program to calculate the sum of all the values of an array](#Q17)
24+
18. [Get the maximum value from a numbers array along with its index](#Q18)
25+
19. [Find the number of occurences of minimum value in the numbers list](#Q19)
26+
20. [Create an array of length n with all the values of it set to 10](#Q20)
27+
21. [Optimize the given statements having lot of logical checks to use a compact and cleaner logic](#Q21)
28+
22. [Write a program to iterate over a 2 dimensional array and print all the values of it](#Q22)
29+
23. [Write a program to store values in to a set](#Q23)
30+
24. [Write a program to store values in to a map](#Q24)
31+
25. [Write a code to iterate over a set](#Q25)
32+
26. [Write a code to iterate over a map](#Q26)
33+
27. [Show how map is different from object to store key value pairs with coding example](#Q27)
34+
28. [Write the code to remove the duplicates from the array](#Q28)
35+
29. [Design a flat function which flattens an array to any depth](#Q29)
36+
30. [Check if all the students of have passed or not (40 is the pass marks)](#Q30)
37+
31. [Get the average of all the salaries which is greater than 10000 from the department of "IT" from the array of objects)](#Q31)
38+
32. [Extract the list of all the elements from the list of numbers given in 2 arrays](#Q32)
39+
33. [Get the list of all distinct elements which are present in both list of numbers](#Q33)
40+
34. [Extract list of elements present only in the first list given.](#Q34)
41+
35. [Create a function named "average" which can calculate the average of an array and should be available to be called from any Array object.](#Q35)
42+
36. [Write a program to polyfill `filter` functionality of the Array](#Q36)
43+
37. [Write a program to polyfill `map` functionality of the Array](#Q37)
44+
38. [Write a program to polyfill `reduce` functionality of the Array](#Q38)
45+
39. [Write a code to eliminate duplicate objects in an array where each object has an 'id' property which can be used to identify the object and the duplicate object with lower rank to be removed](#Q39)
46+
40. [Create an array which will only accept string values. (Homogeneous array of strings)](#Q40)
47+
41. [Create a Proxy object through which the array can be accessed as usual but also allow to access the values through negative indices](#Q41)
4748

4849
---
4950

@@ -333,6 +334,32 @@ If function is not passed an argument, default sorting will happen
333334
<br />
334335

335336
#### Q11
337+
### Sort the given array of objects in ascending order according the authors lastname
338+
```js
339+
// Example
340+
const books = [
341+
{ name: "Harry Potter", author: "Joanne Rowling" },
342+
{ name: "Warcross", author: "Marie Lu" },
343+
{ name: "The Hunger Games", author: "Suzanne Collins" },
344+
]
345+
```
346+
- `sort` takes a function and expects the return value to be an integer for sorting
347+
- The last names of the author can be compared and the result can be returned for sorting
348+
349+
```js
350+
books.sort((book1, book2) => {
351+
const authorLastName1 = book1.author.split(" ")[1];
352+
const authorLastName2 = book2.author.split(" ")[1];
353+
return authorLastName2 > authorLastName1 ? -1 : 1;
354+
});
355+
```
356+
357+
###### Notes
358+
Returning a true or false will not work as the algorithm expects an integer value
359+
360+
<br />
361+
362+
#### Q12
336363
### Square all the positive numbers of the array and return the output array
337364

338365
- `filter` is the method on Array which can be used to filter. It receives a function which can return boolean to filter the elements
@@ -356,7 +383,7 @@ const squaredPositiveArr = arr.filter((value) => value >= 0).map((value) => valu
356383

357384
<br />
358385

359-
#### Q12
386+
#### Q13
360387
### Write a code to generate an array with range of numbers and shuffle them
361388

362389
- An array of numbers in the range can be generated from a function which can take start and end value of the range
@@ -398,7 +425,7 @@ console.log(shuffledArr) // [5, 4, 7, 10, 3, 6, 8, 2
398425

399426
<br />
400427

401-
#### Q13
428+
#### Q14
402429
### Check if the user with the name "John" exists in the array of objects
403430

404431
```js
@@ -417,7 +444,7 @@ const doesJohnExist = jonhIndex < 0 ? false : true;
417444

418445
<br />
419446

420-
#### Q14
447+
#### Q15
421448
### Generate an array of objects with properties id and full name from an array of objects where each object will have id, firstname and lastname
422449

423450
- To manipulate array of objects `map` method can be used
@@ -428,7 +455,7 @@ const employeesListWithFullName = arr.map((obj) => { return { id, fullName: obj.
428455

429456
<br />
430457

431-
#### Q15
458+
#### Q16
432459
### Create an array by removing all the holes of the array
433460

434461
- Holes are `undefined` value present inside array
@@ -443,7 +470,7 @@ Holes can be formed when an array value by index is deleted. Example: `delete ar
443470

444471
<br />
445472

446-
#### Q16
473+
#### Q17
447474
### Write a program to calculate the sum of all the values of an array
448475

449476
- Sum of the values of an array can calculated by iterating and adding all the values of the array
@@ -472,7 +499,7 @@ for(let value of arr){
472499

473500
<br />
474501

475-
#### Q17
502+
#### Q18
476503
### Get the maximum value from a numbers array along with its index
477504

478505
- `Math.max` is a method which returns maximum value from a given list of values
@@ -509,7 +536,7 @@ Though 2nd solution is verbose compared but has good performance
509536

510537
<br />
511538

512-
#### Q18
539+
#### Q19
513540
### Find the number of occurences of minimum value in the numbers list
514541

515542
- `filter` method can be used to fetch all the minimum values and we can get the count of those valuses
@@ -522,7 +549,7 @@ minArr.length; // count of minimum value occure
522549

523550
<br />
524551

525-
#### Q19
552+
#### Q20
526553
### Create an array of length n with all the values of it set to 10
527554

528555
- `fill` is a method on Array prototype which fills all the slots of array with the value given passed as the argument
@@ -541,7 +568,7 @@ If an object is passed the object reference is copied to all the slots and not t
541568

542569
<br />
543570

544-
#### Q20
571+
#### Q21
545572
### Optimize the given statements having lot of logical checks to use a compact and cleaner logic
546573
```js
547574
// Example1
@@ -574,7 +601,7 @@ Generally this use case can be implemented for `if` conditions
574601

575602
<br />
576603

577-
#### Q21
604+
#### Q22
578605
### Write a program to iterate over a 2 dimensional array and print all the values of it
579606

580607
- Arrays can be iterated by using its index to fetch the values
@@ -610,7 +637,7 @@ arr.forEach(rowArr => rowArr.forEach(val => console.log(val)));
610637

611638
<br />
612639

613-
#### Q22
640+
#### Q23
614641
### Write a program to store values in to a set
615642

616643
- Set lets us store unique values of any type
@@ -637,7 +664,7 @@ set; // 1, 2, 3
637664

638665
<br />
639666

640-
#### Q23
667+
#### Q24
641668
### Write a program to store values in to a map
642669

643670
- `Map` holds key-value pairs and remembers the original insertion order of the keys
@@ -666,7 +693,7 @@ Unlike objects, `Map` can have any primitive or object as the key
666693

667694
<br />
668695

669-
#### Q24
696+
#### Q25
670697
### Write a code to iterate over a set
671698

672699
- `set` is an iterable object and can be iterated using for..of loop
@@ -682,7 +709,7 @@ set.forEach(value => console.log(value));
682709

683710
<br />
684711

685-
#### Q25
712+
#### Q26
686713
### Write a code to iterate over a map
687714

688715
- `map` is an iterable object and can be iterated using for..of loop
@@ -702,7 +729,7 @@ map.forEach((value, key) => console.log(key, value));
702729

703730
<br />
704731

705-
#### Q26
732+
#### Q27
706733
### Show how map is different from object to store key value pairs with coding example
707734

708735
- Map does not contain any keys by default unlike objects which has keys from its prototype
@@ -724,7 +751,7 @@ Maps perform better than objects in most of the scenarios involving addition and
724751

725752
<br />
726753

727-
#### Q27
754+
#### Q28
728755
### Write the code to remove the duplicates from the array
729756

730757
- Set is a data structure which does not allow duplicate elements
@@ -736,7 +763,7 @@ const distinctArr = [...set];
736763

737764
<br />
738765

739-
#### Q28
766+
#### Q29
740767
### Design a flat function which flattens an array to any depth
741768

742769
- Flat function can be used to flatten the array by recursive call
@@ -758,7 +785,7 @@ function flat(arr){
758785

759786
<br />
760787

761-
#### Q29
788+
#### Q30
762789
### Check if all the students of have passed or not (40 is the pass marks)
763790

764791
- `every` is a method on Array prototype which returns true only if all the elements condition satisfies the condition
@@ -769,7 +796,7 @@ const isAllPass = students.every((student) => student.marks >= 40);
769796

770797
<br />
771798

772-
#### Q30
799+
#### Q31
773800
### Get the average of all the salaries which is greater than 10000 from the department of "IT" from the array of objects)
774801

775802
```js
@@ -780,7 +807,7 @@ const itAvgSalaryGT10K = itTotalSalaryGT10K / itEmployeesWithSalaryGT10K.length;
780807
781808
<br />
782809
783-
#### Q31
810+
#### Q32
784811
### Extract the list of all the elements from the list of numbers given in 2 arrays
785812
786813
- The union array will be the result if all the elements from the 2 arrays are picked
@@ -793,7 +820,7 @@ const distinctArr = [...set1, ...set2];
793820
794821
<br />
795822
796-
#### Q32
823+
#### Q33
797824
### Get the list of all distinct elements which are present in both list of numbers
798825
799826
- The intersection array will be the result if the common elements from the 2 arrays are picked
@@ -811,7 +838,7 @@ const distinctIntersectionArr = [...set1].filter(value => set2.has(value));
811838
812839
<br />
813840
814-
#### Q33
841+
#### Q34
815842
### Extract list of elements present only in the first list given.
816843
817844
- The only present elements of 1st list will be the result when all the elements of 1st list not present in the 2nd are chosen
@@ -827,7 +854,7 @@ Elements of 2nd list only can be obtained by checking for all the elements of li
827854
828855
<br />
829856
830-
#### Q34
857+
#### Q35
831858
### Create a function named "average" which can calculate the average of an array and should be available to be called from any Array object.
832859
833860
- The function added to Array prototype are accessible to all the objects of Array
@@ -845,7 +872,7 @@ Array.prototype.average = function (){
845872
846873
<br />
847874
848-
#### Q35
875+
#### Q36
849876
### Write a program to polyfill `filter` functionality of the Array
850877
851878
- `filter` iterates over the all values of array and passes value, index and array (itself) as the arguments
@@ -875,7 +902,7 @@ The solution is a simple polyfill of `filter` and not intended to handle all the
875902
876903
<br />
877904
878-
#### Q36
905+
#### Q37
879906
### Write a program to polyfill `map` functionality of the Array
880907
881908
- `map` iterates over the all values of array and passes value, index and array (itself) as the arguments
@@ -902,7 +929,7 @@ The solution is a simple polyfill of `map` and not intended to handle all the co
902929
903930
<br />
904931
905-
#### Q37
932+
#### Q38
906933
### Write a program to polyfill `reduce` functionality of the Array
907934
908935
- `reduce` iterates over the all values of array and passes value, index and array (itself) as the arguments
@@ -928,7 +955,7 @@ The solution is a simple polyfill of `reduce` and not intended to handle all the
928955
929956
<br />
930957
931-
#### Q38
958+
#### Q39
932959
### Write a code to eliminate duplicate objects in an array where each object has an 'id' property which can be used to identify the object and the duplicate object with lower rank to be removed
933960
```js
934961
// Example
@@ -978,7 +1005,7 @@ distinctArr = [...map.values()];
9781005
9791006
<br />
9801007
981-
#### Q39
1008+
#### Q40
9821009
### Create an array which will only accept string values. (Homogeneous array of strings)
9831010
9841011
- Array in JavaScript a collection of values of any type by default
@@ -1006,7 +1033,7 @@ The functionality of the code can be modified to make the array accept any one o
10061033
10071034
<br />
10081035
1009-
#### Q40
1036+
#### Q41
10101037
### Create a Proxy object through which the array can be accessed as usual but also allow to access the values through negative indices
10111038
```js
10121039
// Example

0 commit comments

Comments
 (0)