You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## JavaScript interview code challenges on Functions - challenges
6
-
7
-
1.[Design a Calulator interface for 2 number inputs which can perform sum, difference, product and dividend whenever invoked on the same interface](#Q1)
8
-
1.[Design a private counter function which exposes increment and retrive functionalities](#Q2)
9
-
1.[Write a polyfill for bind function](#Q3)
10
-
1.[Write a function which will create a function bounded to the context like `bind`, but can be overridden when the context is set explicitly](#Q4)
11
-
1.[Write a function which helps to achieve multiply(a)(b) and returns product of a and b](#Q5)
12
-
1.[Create a function which takes another function as an argument and makes it eligible for currying or partial application](#Q6)
13
-
1.[Design a function which helps to do debouncing](#Q7)
14
-
1.[Design a function which helps to do throttling](#Q8)
15
-
1.[Design an interface which limits the number of function calls by executing the function once for a given count of calls](#Q9)
16
-
1.[Write a singleton function to create an object only once](#Q10)
17
-
1.[Design a function with toggle functionality for given list of inputs where toggle function accepts list of values to be toggled upon](#Q11)
18
-
1.[Create a range function which returns an array for the provided inputs as start and end](#Q12)
19
-
1.[Write a function which takes a function as an argument to achieve memoization](#Q13)
20
-
1.[Create a single function which can perform sum(a, b, c), sum(a, b)(c), sum(a)(b, c) and sum(a)(b)(c) and returns sum of a, b and c](#Q14)
21
-
1.[Design a function which can keep recieving the arguments on each function call and returns the sum when no argument is passed](#Q15)
22
-
1.[Create an interface for a function such that whenever a function is triggered the system should log the time. Do not modify the function code](#Q16)
23
-
1.[Create an interface exposing subscribe and publish functionality, which allows publishing data which in turn invokes all the subscribers with the data](#Q17)
24
-
25
-
---
26
-
27
-
#### Q1
28
-
### Design a Calulator interface for 2 number inputs which can perform sum, difference, product and dividend whenever invoked on the same interface
5
+
<h2align="center">JavaScript challenges on Functions - challenges</h2>
6
+
7
+
<br>
8
+
9
+
### Q. Design a Calulator interface for 2 number inputs which can perform sum, difference, product and dividend whenever invoked on the same interface
29
10
```js
30
11
// Example
31
12
constcalc12And5=Calculator(12, 5);
@@ -68,8 +49,7 @@ The solution provided is one of the way to achieve the interface. The design and
68
49
69
50
<br />
70
51
71
-
#### Q2
72
-
### Design a private counter function which exposes increment and retrive functionalities
52
+
### Q. Design a private counter function which exposes increment and retrive functionalities
73
53
74
54
```js
75
55
functionprivateCounter(){
@@ -102,8 +82,7 @@ counter.retrieve(); // 8
102
82
103
83
<br />
104
84
105
-
#### Q3
106
-
### Write a polyfill for bind function
85
+
### Q. Write a polyfill for bind function
107
86
108
87
- The `bind` method creates a new function that, when called, has its this keyword set to the provided context
109
88
@@ -129,8 +108,7 @@ This is a simple polyfill for bind without handling corner cases. It does not wo
129
108
130
109
<br />
131
110
132
-
#### Q4
133
-
### Write a function which will create a function bounded to the context like `bind`, but can be overridden when the context is set explicitly
111
+
### Q. Write a function which will create a function bounded to the context like `bind`, but can be overridden when the context is set explicitly
134
112
135
113
- The functionality is similar to `bind` with exception that if there is a context set during the execution it will override
136
114
@@ -157,8 +135,7 @@ This functionality is also known as 'Soft Binding'
157
135
158
136
<br />
159
137
160
-
#### Q5
161
-
### Write a function which helps to achieve multiply(a)(b) and returns product of a and b
138
+
### Q. Write a function which helps to achieve multiply(a)(b) and returns product of a and b
162
139
```js
163
140
// Example
164
141
multiply(2)(4); // 8
@@ -180,8 +157,7 @@ function multiply(num1){
180
157
181
158
<br />
182
159
183
-
#### Q6
184
-
### Create a function which takes another function as an argument and makes it eligible for currying or partial application
160
+
### Q. Create a function which takes another function as an argument and makes it eligible for currying or partial application
185
161
186
162
- Function can take another function as argument and accept the arguments on the returned functions till the expected arguments are reached
187
163
- The arguments can be 1 or multiple, and the actual function will be called once the count of expected arguments are reached
@@ -213,8 +189,7 @@ sum(1,2)(3,4); // called like partial application
213
189
214
190
<br />
215
191
216
-
#### Q7
217
-
### Design a function which helps to do debouncing
192
+
### Q. Design a function which helps to do debouncing
218
193
219
194
- The `debounce` function forces a function to wait a certain amount of time before running again
220
195
- The function is built to limit the number of function calls to improve the performance
@@ -244,8 +219,7 @@ Context is replaced while debounce function call in presence of a context. If no
244
219
245
220
<br />
246
221
247
-
#### Q8
248
-
### Design a function which helps to do throttling
222
+
### Q. Design a function which helps to do throttling
249
223
250
224
- The `throttling` function forces a function to run once in an amount of time for one or multiple calls
251
225
- The function is built to limit the number of function calls to improve the performance
@@ -279,8 +253,7 @@ Last arguments to the throttled function is saved so that most recent arguments
279
253
280
254
<br />
281
255
282
-
#### Q9
283
-
### Design an interface which limits the number of function calls by executing the function once for a given count of calls
256
+
### Q. Design an interface which limits the number of function calls by executing the function once for a given count of calls
284
257
285
258
- function forces a function run to for specific number of times in a given number of execution calls
286
259
- The function is built to limit the number of times a function is called
@@ -310,8 +283,7 @@ Sampling is different from throttling in the way that sampling limits the execut
310
283
311
284
<br />
312
285
313
-
#### Q10
314
-
### Write a singleton function to create an object only once
286
+
### Q. Write a singleton function to create an object only once
315
287
316
288
- Singleton is a design pattern which restricts the creation of only one object from a given interface
317
289
- When requested multiple times, same object is returned
@@ -348,8 +320,7 @@ Here both 'instance1' and 'instace2' are referencing to the same object
348
320
349
321
<br />
350
322
351
-
#### Q11
352
-
### Design a function with toggle functionality for given list of inputs where toggle function accepts list of values to be toggled upon
323
+
### Q. Design a function with toggle functionality for given list of inputs where toggle function accepts list of values to be toggled upon
353
324
```js
354
325
// Example
355
326
var hello =toggle("hello");
@@ -388,8 +359,7 @@ function toggle(...values){
388
359
389
360
<br />
390
361
391
-
#### Q12
392
-
### Create a range function which returns an array for the provided inputs as start and end
362
+
### Q. Create a range function which returns an array for the provided inputs as start and end
393
363
```js
394
364
// Example
395
365
range(3, 6) // [3, 4, 5, 6]
@@ -421,8 +391,7 @@ function range(start, end) {
421
391
422
392
<br />
423
393
424
-
#### Q13
425
-
### Write a function which takes a function as an argument to achieve memoization
394
+
### Q. Write a function which takes a function as an argument to achieve memoization
426
395
427
396
- Memoization is an optimization technique used primarily to speed up the programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again
428
397
- Function can be designed to use a cache storage (using `map` or `object`) which stores the values of function output against the input
@@ -461,8 +430,7 @@ Stringification of arguments done in order for the function to work for multiple
461
430
462
431
<br />
463
432
464
-
#### Q14
465
-
### Create a single function which can perform sum(a, b, c), sum(a, b)(c), sum(a)(b, c) and sum(a)(b)(c) and returns sum of a, b and c
433
+
### Q. Create a single function which can perform sum(a, b, c), sum(a, b)(c), sum(a)(b, c) and sum(a)(b)(c) and returns sum of a, b and c
466
434
```js
467
435
// Example
468
436
sum(2)(4)(6); // 12
@@ -516,8 +484,7 @@ function sum() {
516
484
517
485
<br />
518
486
519
-
#### Q15
520
-
### Design a function which can keep recieving the arguments on each function call and returns the sum when no argument is passed
487
+
### Q. Design a function which can keep recieving the arguments on each function call and returns the sum when no argument is passed
521
488
522
489
- The function can be designed to return another function which maintains the closure over the previous sum value
523
490
- The check for breaking condition can be added using the argument check for `undefined`
@@ -565,8 +532,7 @@ In the code value is checked if it is undefined reason being 0 is a falsy value
565
532
566
533
<br />
567
534
568
-
#### Q16
569
-
### Create an interface for a function such that whenever a function is triggered the system should log the time. Do not modify the function code
535
+
### Q. Create an interface for a function such that whenever a function is triggered the system should log the time. Do not modify the function code
570
536
571
537
- Function call can be handled using Proxy in JavaScript
572
538
- `apply` keyword in proxy can be used to achieve the functionality without modifying the existing function code
@@ -596,8 +562,7 @@ This technique is helpful in logging or managing the data being passed to & retu
596
562
597
563
<br />
598
564
599
-
#### Q17
600
-
### Create an interface exposing subscribe and publish functionality, which allows publishing data which in turn invokes all the subscribers with the data
565
+
### Q. Create an interface exposing subscribe and publish functionality, which allows publishing data which in turn invokes all the subscribers with the data
601
566
602
567
- A simple module with publish and subscribe function can be exposed to achieve such functionality
603
568
- List of subscribers can be maintained in an array and can be invoked in loop on each publish
0 commit comments