Skip to content

Commit 4d2806a

Browse files
author
Sadanand Pai
committed
moved the regex to object
1 parent 7f1bdd9 commit 4d2806a

File tree

3 files changed

+78
-77
lines changed

3 files changed

+78
-77
lines changed

challenges/objects-challenges.md

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@
1010
1. [Create an array of pair of values (key, value) from an object and store it in a map. Consider the object is not nested](#Q4)
1111
1. [Create an object with a property 'marks' which cannot be set to a value less than 0](#Q5)
1212
1. [Create an object which has a property 'userid' which can only be set once and will be a read only property](#Q6)
13-
1. [Design a function which takes an array as input and returns a function 'next', calling which fetches a value one by one](#Q7)
14-
1. [Create an object 'obj' with functions assigned to keys. Show how can we achieve 'obj.func1().func2().func3()' considering func1, func2, func3 are object keys](#Q8)
15-
1. [Create an object with property counter which keeps incrementing on every access](#Q9)
16-
1. [Create an object and make it behave like an array which allows push and pop operations on items](#Q10)
17-
1. [Write a function which can be used to deeply compare 2 nested objects](#Q11)
18-
1. [Design a class for employee which takes id and name in during construction of object and has a salary property](#Q12)
19-
1. [Write a program to make all the properties of an object ready only but allow the addition of new properties](#Q13)
20-
1. [Write a program which can return a boolean if value is present in the range with given start and end values in an object](#Q14)
21-
1. [Write a function which accepts a topic and a list of related tags to store the information. The same function should return all the topics when requested with a tagname](#Q15)
22-
1. [Write a function which accepts a collection of values & an iteratee as arguments and returns a grouped object](#Q16)
23-
1. [Create a constructor function which allows its functions present on prototype to be accessed only by the objects created by calling it](#Q17)
24-
1. [Design a utility on an array of objects where the access can be made to the object using index (as usual) and also from primary key of the object](#Q18)
25-
1. [Write a function which receives an object and returns a true if the object has circular reference](#Q19)
26-
1. [Write a code which can eliminate circular references in an object (Cyclic reference in an object)](#Q20)
27-
1. [Provide an object on which a value can be set to nested property even if it does not exist](#Q21)
13+
1. [Stringify an object by excluding the 'password' property](#Q7)
14+
1. [Design a function which takes an array as input and returns a function 'next', calling which fetches a value one by one](#Q8)
15+
1. [Create an object 'obj' with functions assigned to keys. Show how can we achieve 'obj.func1().func2().func3()' considering func1, func2, func3 are object keys](#Q9)
16+
1. [Create an object with property counter which keeps incrementing on every access](#Q10)
17+
1. [Create an object and make it behave like an array which allows push and pop operations on items](#Q11)
18+
1. [Write a function which can be used to deeply compare 2 nested objects](#Q12)
19+
1. [Design a class for employee which takes id and name in during construction of object and has a salary property](#Q13)
20+
1. [Write a program to make all the properties of an object ready only but allow the addition of new properties](#Q14)
21+
1. [Write a program which can return a boolean if value is present in the range with given start and end values in an object](#Q15)
22+
1. [Write a function which accepts a topic and a list of related tags to store the information. The same function should return all the topics when requested with a tagname](#Q16)
23+
1. [Write a function which accepts a collection of values & an iteratee as arguments and returns a grouped object](#Q17)
24+
1. [Create a constructor function which allows its functions present on prototype to be accessed only by the objects created by calling it](#Q18)
25+
1. [Design a utility on an array of objects where the access can be made to the object using index (as usual) and also from primary key of the object](#Q19)
26+
1. [Write a function which receives an object and returns a true if the object has circular reference](#Q20)
27+
1. [Write a code which can eliminate circular references in an object (Cyclic reference in an object)](#Q21)
28+
1. [Provide an object on which a value can be set to nested property even if it does not exist](#Q22)
2829

2930
---
3031

@@ -201,6 +202,35 @@ const obj = userObjectCreator(1);
201202
<br />
202203

203204
#### Q7
205+
### Stringify an object by excluding the 'password' property
206+
```js
207+
// Example
208+
const obj = {
209+
id: 1,
210+
username: 'John',
211+
password: 'secret',
212+
email: 'john@email.com',
213+
};
214+
```
215+
216+
- `JSON.stringify` is the method which can be used for stringification of an object or any other value
217+
- It accepts 2nd argument which can be a function or array
218+
219+
```js
220+
JSON.stringify(obj, (key, value) => key === 'password' ? undefined : value); // {"id":1,"username":"John","email":"john@email.com"}
221+
```
222+
223+
```js
224+
JSON.stringify(obj, ['id', 'username', 'email']); // {"id":1,"username":"John","email":"john@email.com"}
225+
```
226+
227+
###### References
228+
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
229+
230+
<br />
231+
232+
233+
#### Q8
204234
### Design a function which takes an array as input and returns a function 'next', calling which fetches a value one by one
205235

206236
- The function returned `next` will return an object which contains value and done properties
@@ -234,7 +264,7 @@ it.next().done; // true
234264

235265
<br />
236266

237-
#### Q8
267+
#### Q9
238268
### Create an object 'obj' with functions assigned to keys. Show how can we achieve 'obj.func1().func2().func3()' considering func1, func2, func3 are object keys
239269

240270
- For achieving chaining functionality, each function can return the calling context itself so that context is retained
@@ -276,7 +306,7 @@ Order of calling the functions does not matter as all the functions are returnin
276306

277307
<br />
278308

279-
#### Q9
309+
#### Q10
280310
### Create an object with property counter which keeps incrementing on every access
281311
```js
282312
const obj = counterObject();
@@ -312,7 +342,7 @@ Symbol is used to maintain the private variable in the object. Using the private
312342

313343
<br />
314344

315-
#### Q10
345+
#### Q11
316346
### Create an object and make it behave like an array which allows push and pop operations on items
317347

318348
- Object does not have by default a property named 'length' and hence we can define it on object which helps to track the length
@@ -341,7 +371,7 @@ As the context for array methods is set object, length of the object changes whe
341371

342372
<br />
343373

344-
#### Q11
374+
#### Q12
345375
### Write a function which can be used to deeply compare 2 nested objects
346376
```js
347377
// Example
@@ -396,7 +426,7 @@ Stringification of both objects and comparision will also work, but fails on key
396426

397427
<br />
398428

399-
#### Q12
429+
#### Q13
400430
### Design a class for employee which takes id and name in during construction of object and has a salary property
401431

402432
- Classes are a template for creating objects. They encapsulate data with code to work on that data
@@ -444,7 +474,7 @@ Class in JavaScript is functionality to achieve class based model on top of prot
444474

445475
<br />
446476

447-
#### Q13
477+
#### Q14
448478
### Write a program to make all the properties of an object ready only but allow the addition of new properties
449479

450480
- The exisiting properties of the object can be made read only with `set` keyword using Proxy
@@ -472,7 +502,7 @@ If condition takes care whether the property is new or existing to handle the re
472502

473503
<br />
474504

475-
#### Q14
505+
#### Q15
476506
### Write a program which can return a boolean if value is present in the range with given start and end values in an object
477507
```js
478508
// Example
@@ -497,7 +527,7 @@ range = new Proxy(range, {
497527

498528
<br />
499529

500-
#### Q15
530+
#### Q16
501531
### Write a function which accepts a topic and a list of related tags to store the information. The same function should return all the topics when requested with a tagname
502532
```js
503533
// Example
@@ -542,7 +572,7 @@ function TagManager() {
542572

543573
<br />
544574

545-
#### Q16
575+
#### Q17
546576
### Write a function which accepts a collection of values & an iteratee as arguments and returns a grouped object
547577
```js
548578
// Example
@@ -570,7 +600,7 @@ function groupBy(values, iteratee) {
570600

571601
<br />
572602

573-
#### Q17
603+
#### Q18
574604
### Create a constructor function which allows its functions present on prototype to be accessed only by the objects created by calling it
575605

576606
- The list of objects created by the function can be kept in track using a collection object inside function
@@ -600,7 +630,7 @@ ProtectedFunction.prototype.method.call(obj); // Incompatible object!
600630

601631
<br />
602632

603-
#### Q18
633+
#### Q19
604634
### Design a utility on an array of objects where the access can be made to the object using index (as usual) and also from primary key of the object
605635
```js
606636
// Example
@@ -633,7 +663,7 @@ const flexEmployees = new Proxy(employees, {
633663

634664
<br />
635665

636-
#### Q19
666+
#### Q20
637667
### Write a function which receives an object and returns a true if the object has circular reference
638668
```js
639669
// Example
@@ -660,7 +690,7 @@ function doesObjectHaveCircularRef(obj){
660690

661691
<br />
662692

663-
#### Q20
693+
#### Q21
664694
### Write a code which can eliminate circular references in an object (Cyclic reference in an object)
665695

666696
- Circular / cyclic reference exists when the object property value forms a cycle
@@ -708,7 +738,7 @@ function removeCircularRef(obj) {
708738

709739
<br />
710740

711-
#### Q21
741+
#### Q22
712742
### Provide an object on which a value can be set to nested property even if it does not exist.
713743

714744
- The nested object can be accessed only if all the nested properties are defined on the object

challenges/objects-concepts.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1. [Show the different types of accessor properties available for object property and write a code defining them](#Q9)
1616
1. [Show the different options available to prevent the modifications to the object](#Q10)
1717
1. [Modify the given object so that it can be used inside a for...of loop](#Q11)
18-
1. [Stringify an object by excluding the 'password' property](#Q12)
18+
1. [Show the creation of Regular Expression in JavaScript](#Q12)
1919
1. [Write a polyfill for Object.create](#Q13)
2020
1. [Write a code show Optional chaining for objects and functions](#Q14)
2121
1. [Show the usage of static variable & function in a class and accessing it from the code](#Q15)
@@ -355,30 +355,28 @@ const obj = {
355355
<br />
356356

357357
#### Q12
358-
### Stringify an object by excluding the 'password' property
359-
```js
360-
// Example
361-
const obj = {
362-
id: 1,
363-
username: 'John',
364-
password: 'secret',
365-
email: 'john@email.com',
366-
};
367-
```
358+
### Show the creation of Regular Expression in JavaScript
368359

369-
- `JSON.stringify` is the method which can be used for stringification of an object or any other value
370-
- It accepts 2nd argument which can be a function or array
360+
- Regular expressions are patterns used to match character combinations in strings
361+
- Regular expressions can be created using literal form or constructor form
362+
- Constructor form accepts regular expression as the first argument and flags as the 2nd argument
363+
- Literal form is simple which takes regular expression and flags in a single expression
371364

372365
```js
373-
JSON.stringify(obj, (key, value) => key === 'password' ? undefined : value); // {"id":1,"username":"John","email":"john@email.com"}
366+
// literal form
367+
let re = /ab+c/g;
374368
```
375369

376370
```js
377-
JSON.stringify(obj, ['id', 'username', 'email']); // {"id":1,"username":"John","email":"john@email.com"}
371+
// constructor form
372+
let re = new RegExp('ab+c', 'g');
378373
```
379374

375+
###### Notes
376+
In JavaScript, regular expressions are objects
377+
380378
###### References
381-
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
379+
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
382380

383381
<br />
384382

challenges/primitives-concepts.md

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
1. [Show the frequently and commonly used methods available on `Math` object with coding examples](#Q8)
1515
1. [How can we solve the problem of comparision of 0.1 + 0.2 with 0.3 where `===` operator fails](#Q9)
1616
1. [Write a code to iterate over a string](#Q10)
17-
1. [Show the creation of Regular Expression in JavaScript](#Q11)
18-
1. [Show the usage of template literals, expression interpolation and tagged templates](#Q12)
19-
1. [Write a code to show the working of `try...catch...finally`](#Q13)
20-
1. [Show the creation and usage of `symbol` with code](#Q14)
17+
1. [Show the usage of template literals, expression interpolation and tagged templates](#Q11)
18+
1. [Write a code to show the working of `try...catch...finally`](#Q12)
19+
1. [Show the creation and usage of `symbol` with code](#Q13)
2120

2221
---
2322

@@ -330,32 +329,6 @@ for(let value of str){
330329
<br />
331330
332331
#### Q11
333-
### Show the creation of Regular Expression in JavaScript
334-
335-
- Regular expressions are patterns used to match character combinations in strings
336-
- Regular expressions can be created using literal form or constructor form
337-
- Constructor form accepts regular expression as the first argument and flags as the 2nd argument
338-
- Literal form is simple which takes regular expression and flags in a single expression
339-
340-
```js
341-
// literal form
342-
let re = /ab+c/g;
343-
```
344-
345-
```js
346-
// constructor form
347-
let re = new RegExp('ab+c', 'g');
348-
```
349-
350-
###### Notes
351-
In JavaScript, regular expressions are objects
352-
353-
###### References
354-
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
355-
356-
<br />
357-
358-
#### Q12
359332
### Show the usage of template literals with expression interpolation and tagged templates
360333
361334
- Template literals are string literals allowing embedded expressions and support multi lines
@@ -391,7 +364,7 @@ myTag`Note: ${person} is a member of following communities: ${membership}`;
391364
392365
<br />
393366
394-
#### Q13
367+
#### Q12
395368
### Write a code to show the working of `try...catch...finally`
396369
397370
- The `try` statement consists of a try-block, which contains one or more statements. At least one catch-block, or a finally-block, must be present
@@ -418,7 +391,7 @@ try {
418391
419392
<br />
420393
421-
#### Q14
394+
#### Q13
422395
### Show the creation and usage of `symbol` with code
423396
424397
- A "symbol" represents a unique identifier

0 commit comments

Comments
 (0)