2
2
<a href =" /README.md#this-is-a-collection-of-modern-interview-code-challenges-on-javascript-suitable-for " id =" home " >Home</a >
3
3
</div >
4
4
5
- ## JavaScript interview code challenges on Collections - concepts
6
-
7
- 1 . [ Show the different ways of creating an array] ( #Q1 )
8
- 1 . [ Write a program to iterate over an array and print all the values of it] ( #Q2 )
9
- 1 . [ Write a program to append and prepend, single or multiple values in to an array] ( #Q3 )
10
- 1 . [ Show how insertion and removal of elements can happen in the array for given index] ( #Q4 )
11
- 1 . [ Show the different ways of emptying an array which has values] ( #Q5 )
12
- 1 . [ Check if given input is an array or not] ( #Q6 )
13
- 1 . [ Show how an array in JavaScript can act like a stack and queue] ( #Q7 )
14
- 1 . [ Create an array by removing all the holes of the array] ( #Q8 )
15
- 1 . [ Optimize the given statements having lot of logical checks to use a compact and cleaner logic] ( #Q9 )
16
- 1 . [ Write a program to iterate over a 2 dimensional array and print all the values of it] ( #Q10 )
17
- 1 . [ Write a program to store values in to a set] ( #Q11 )
18
- 1 . [ Write a program to store values in to a map] ( #Q12 )
19
- 1 . [ Write a code to iterate over a set] ( #Q13 )
20
- 1 . [ Write a code to iterate over a map] ( #Q14 )
21
- 1 . [ Show how map is different from object to store key value pairs with coding example] ( #Q15 )
22
- 1 . [ Write a program to polyfill ` filter ` functionality of the Array] ( #Q16 )
23
- 1 . [ Write a program to polyfill ` map ` functionality of the Array] ( #Q17 )
24
- 1 . [ Write a program to polyfill ` reduce ` functionality of the Array] ( #Q18 )
25
-
26
- ---
27
-
28
- #### Q1
29
- ### Show the different ways of creating an array
5
+ <h2 align =" center " >JavaScript challenges on Collections - concepts</h2 >
6
+
7
+ <br >
8
+
9
+ ### Q. Show the different ways of creating an array
30
10
31
11
- Arrays are the collection of values in javascript. Array is a special type of object in JavaScript
32
12
- Arrays values are indexed from 0 and have special property length which stores the count of elements present in array
@@ -61,8 +41,7 @@ const arr = new Array(1, true, "string");
61
41
62
42
<br />
63
43
64
- #### Q2
65
- ### Write a program to iterate over an array and print all the values of it
44
+ ### Q. Write a program to iterate over an array and print all the values of it
66
45
67
46
- Arrays can be iterated by using its index to fetch the values
68
47
- Arrays also can be iterated with for each style loops
@@ -91,8 +70,7 @@ arr.forEach(val => console.log(val));
91
70
92
71
<br />
93
72
94
- #### Q3
95
- ### Write a program to append and prepend, single or multiple values in to an array
73
+ ### Q. Write a program to append and prepend, single or multiple values in to an array
96
74
97
75
- Values to the array can be appended using ` push ` method of array
98
76
- Values to the array can be prepended using ` unshift ` method of array
@@ -128,8 +106,7 @@ To remove the elements from the end of the array `pop` operation can be used but
128
106
129
107
<br />
130
108
131
- #### Q4
132
- ### Show insertion and removal of elements can happen in the array for given index
109
+ ### Q. Show insertion and removal of elements can happen in the array for given index
133
110
134
111
- Values of the array can be removed from any position using ` splice ` method of array
135
112
- Values of the array can also be inserted to any position using ` splice ` method of array
@@ -159,8 +136,7 @@ console.log(arr); // [1, 2, 3, 4, 5]
159
136
160
137
<br />
161
138
162
- #### Q5
163
- ### Show the different ways of emptying an array which has values
139
+ ### Q. Show the different ways of emptying an array which has values
164
140
165
141
- Array can be emptied by giving a new reference of an empty array
166
142
- Setting the ` length ` of the array to 0 will automatically makes the array empty
@@ -192,8 +168,7 @@ arr.splice(0, arr.length)
192
168
193
169
<br />
194
170
195
- #### Q6
196
- ### Check if given input is an array or not
171
+ ### Q. Check if given input is an array or not
197
172
198
173
- ` Array.isArray ` is a method which checks if the given argument is an array or not
199
174
- Alternatively the ` toString ` method present on Object prototype can be used to check if it is an array
@@ -214,8 +189,7 @@ Object.prototype.toString.call(arr) === '[object Array]'
214
189
215
190
<br />
216
191
217
- #### Q7
218
- ### Show how an array in JavaScript can act like a stack and queue
192
+ ### Q. Show how an array in JavaScript can act like a stack and queue
219
193
220
194
- Stack is a 'Last In First Out' data structure can be achieved using ` push ` and ` pop ` operations
221
195
- Queue is a 'First In First Out' data structure can be achieved using ` push ` and ` shift ` operations
@@ -238,8 +212,7 @@ arr.shift();
238
212
239
213
<br />
240
214
241
- #### Q8
242
- ### Create an array by removing all the holes of the array
215
+ ### Q. Create an array by removing all the holes of the array
243
216
244
217
- Holes are ` undefined ` value present inside array
245
218
- Holes do not get iterated in ` filter ` which will just fetch all the values except ` undefined `
@@ -253,8 +226,7 @@ Holes can be formed when an array value by index is deleted. Example: `delete ar
253
226
254
227
<br />
255
228
256
- #### Q9
257
- ### Optimize the given statements having lot of logical checks to use a compact and cleaner logic
229
+ ### Q. Optimize the given statements having lot of logical checks to use a compact and cleaner logic
258
230
``` js
259
231
// Example1
260
232
browser === " chrome" || browser === " firefox" || browser === " IE" || browser === " safari"
@@ -286,8 +258,7 @@ Generally this use case can be implemented for `if` conditions
286
258
287
259
<br />
288
260
289
- #### Q10
290
- ### Write a program to iterate over a 2 dimensional array and print all the values of it
261
+ ### Q. Write a program to iterate over a 2 dimensional array and print all the values of it
291
262
292
263
- Arrays can be iterated by using its index to fetch the values
293
264
- Arrays also can be iterated with for each style loops, with one loop to iterate the rows and inside it for cells
@@ -322,8 +293,7 @@ arr.forEach(rowArr => rowArr.forEach(val => console.log(val)));
322
293
323
294
<br />
324
295
325
- #### Q11
326
- ### Write a program to store values in to a set
296
+ ### Q. Write a program to store values in to a set
327
297
328
298
- Set lets us store unique values of any type
329
299
- Set can be created empty & then added with values or can be initialized also
@@ -349,8 +319,7 @@ set; // 1, 2, 3
349
319
350
320
<br />
351
321
352
- #### Q12
353
- ### Write a program to store values in to a map
322
+ ### Q. Write a program to store values in to a map
354
323
355
324
- ` Map ` holds key-value pairs and remembers the original insertion order of the keys
356
325
- ` Map ` can be created empty & then added with values or can be initialized also with key-value pairs
@@ -378,8 +347,7 @@ Unlike objects, `Map` can have any primitive or object as the key
378
347
379
348
<br />
380
349
381
- #### Q13
382
- ### Write a code to iterate over a set
350
+ ### Q. Write a code to iterate over a set
383
351
384
352
- ` set ` is an iterable object and can be iterated using for..of loop
385
353
- ` set ` can also be iterated by simple ` forEach ` loop
@@ -394,8 +362,7 @@ set.forEach(value => console.log(value));
394
362
395
363
<br />
396
364
397
- #### Q14
398
- ### Write a code to iterate over a map
365
+ ### Q. Write a code to iterate over a map
399
366
400
367
- ` map ` is an iterable object and can be iterated using for..of loop
401
368
- ` map ` can also be iterated by simple ` forEach ` loop
@@ -414,8 +381,7 @@ map.forEach((value, key) => console.log(key, value));
414
381
415
382
<br />
416
383
417
- #### Q15
418
- ### Show how map is different from object to store key value pairs with coding example
384
+ ### Q. Show how map is different from object to store key value pairs with coding example
419
385
420
386
- Map does not contain any keys by default unlike objects which has keys from its prototype
421
387
- Map's keys can be any value (including functions, objects, or any primitive) unlike object where keys are only strings
@@ -436,8 +402,7 @@ Maps perform better than objects in most of the scenarios involving addition and
436
402
437
403
<br />
438
404
439
- #### Q16
440
- ### Write a program to polyfill ` filter ` functionality of the Array
405
+ ### Q. Write a program to polyfill ` filter ` functionality of the Array
441
406
442
407
- ` filter ` iterates over the all values of array and passes value, index and array (itself) as the arguments
443
408
- Function returns a new array which filtering the values of the original array
@@ -466,8 +431,7 @@ The solution is a simple polyfill of `filter` and not intended to handle all the
466
431
467
432
<br />
468
433
469
- #### Q17
470
- ### Write a program to polyfill ` map ` functionality of the Array
434
+ ### Q. Write a program to polyfill ` map ` functionality of the Array
471
435
472
436
- ` map ` iterates over the all values of array and passes value, index and array (itself) as the arguments
473
437
- Function returns a new array which is same as the length of the original array
@@ -493,8 +457,7 @@ The solution is a simple polyfill of `map` and not intended to handle all the co
493
457
494
458
<br />
495
459
496
- #### Q18
497
- ### Write a program to polyfill ` reduce ` functionality of the Array
460
+ ### Q. Write a program to polyfill ` reduce ` functionality of the Array
498
461
499
462
- ` reduce ` iterates over the all values of array and passes value, index and array (itself) as the arguments
500
463
- ` reduce ` accepts an optional initial value which when not provided can be skipped
0 commit comments