22 <a href =" /README.md#collection-of-modern-interview-code-challenges-on-javascript-suitable-for " id =" home " >Home</a >
33</div >
44
5- ## JavaScript interview code challenges on Document Object Model
6-
7- 1 . [ Show the different ways of selecting an element from DOM] ( #Q1 )
8- 1 . [ Show the ways to loop over the Nodelist obtained after querying for the elements] ( #Q2 )
9- 1 . [ Design and Implement a Node Store, which supports DOM element as key] ( #Q3 )
10- 1 . [ Implement a function to find the closest ancestor with the provided selector] ( #Q4 )
11- 1 . [ Write a function to find the corresponding node in two identical DOM trees] ( #Q5 )
12- 1 . [ Write a function to get depth of a given DOM tree] ( #Q6 )
13- 1 . [ Implement a function to get the root node of a given DOM fragment (document.getRootNode() method)] ( #Q7 )
14- 1 . [ Implement a function to get the root node of a given DOM fragment] ( #Q8 )
15- 1 . [ Implement a function to get unique tag names in a given DOM tree] ( #Q9 )
16- 1 . [ Implement a function to check if a given DOM tree has duplicate IDs] ( #Q10 )
17-
18- ---
19-
20- #### Q1
21- ### Show the different ways of selecting an element from DOM
22-
23- - The ` typeof ` operator returns a string indicating the type of the operand
5+ <h2 align =" center " >JavaScript challenges on Document Object Model</h2 >
6+
7+ <br >
8+
9+ ### Q. Show the different ways of selecting an element from DOM
10+
11+ - The element can be selected using its tagname, id, attribute, value etc
2412
2513``` js
26- // querySelector, getElementById etc
14+ document .getElementById (" elementId" ); // elementId is id of the element
15+ document .querySelector (" .my-class" ); // element having a class 'my-class'
2716```
2817
2918###### Notes
30- Note goes here
19+
20+ Above selectors are used to select a first matching element reference even if mutliple matches be there
3121
3222###### References
33- -
23+
24+ - https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
25+ - https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById
3426
3527<br />
3628
37- #### Q2
38- ### Show the ways to loop over the Nodelist obtained after querying for the elements
29+ ### Q. Show the ways to loop over the Nodelist obtained after querying for the elements
3930
40- - The ` typeof ` operator returns a string indicating the type of the operand
31+ - The Nodelist can be obtained by queryng for multiple elements
32+ - As list of elements are array like object (not pure JS array), all the array methods can't be directly used
4133
4234``` js
43- // for..in
35+ // Lets assume DOM elements are fetched in the variable domElements
36+ for (let element of domElements) {
37+ // perform operation on element
38+ }
4439```
4540
46- ###### Notes
47- Note goes here
48-
4941###### References
50- -
42+
43+ - https://developer.mozilla.org/en-US/docs/Web/API/NodeList
5144
5245<br />
5346
54- #### Q3
55- ### Design and Implement a Node Store, which supports DOM element as key
47+ ### Q. Design and Implement a Node Store, which supports DOM element as key
5648
5749- Implement it without using inbuilt Map
5850- Can you do it in O(1) Time complexity?
5951
6052``` js
6153class NodeStore {
62-
6354 constructor () {
6455 this .store = {};
6556 }
66- /**
57+ /**
6758 * @param {Node} node
6859 * @param {any} value
6960 */
7061 set (node , value ) {
71- node .__nodeIdentifier__ = Symbol ();
72- this .store [node .__nodeIdentifier__ ] = value
62+ node .__nodeIdentifier__ = Symbol ();
63+ this .store [node .__nodeIdentifier__ ] = value;
7364 }
7465 /**
7566 * @param {Node} node
@@ -78,7 +69,7 @@ class NodeStore {
7869 get (node ) {
7970 return this .store [node .__nodeIdentifier__ ];
8071 }
81-
72+
8273 /**
8374 * @param {Node} node
8475 * @return {Boolean}
@@ -90,17 +81,17 @@ class NodeStore {
9081```
9182
9283###### References
84+
9385- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
9486
9587<br />
9688
97- #### Q4
98- ### Implement a function to find the closest ancestor with the provided selector (Element.closest() method)
89+ ### Q. Implement a function to find the closest ancestor with the provided selector (Element.closest() method)
9990
10091- The closest() method traverses the Element and its parents (heading toward the document root) until it finds a node that matches the provided selector string. Will return itself or the matching ancestor. If no such element exists, it returns null.
10192
10293``` js
103- Element .prototype .closest = function (selector ) {
94+ Element .prototype .closest = function (selector ) {
10495 var el = this ;
10596 while (el) {
10697 if (el .matches (selector)) {
@@ -113,17 +104,17 @@ Element.prototype.closest = function(selector) {
113104```
114105
115106###### References
107+
116108- https://developer.mozilla.org/en-US/docs/Web/API/Element/closest
117109
118110<br />
119111
120- #### Q5
121- ### Write a function to find the corresponding node in two identical DOM trees
112+ ### Q. Write a function to find the corresponding node in two identical DOM trees
122113
123114- Given two same DOM tree A, B, and an Element a in A, find the corresponding Element b in B. By corresponding, we mean a and b have the same relative position to their DOM tree root.
124115
125116``` js
126- const A = document .createElement (' div' )
117+ const A = document .createElement (" div" );
127118A .innerHTML = `
128119<div >
129120<div >
@@ -138,72 +129,74 @@ A.innerHTML = `
138129 </div >
139130 </div >
140131<div >
141- </div >`
132+ </div >` ;
142133
143- const B = A .cloneNode (true )
144- const node1 = A .querySelector (' #node1' )
145- const node2 = A .querySelector (' #node2' )
146- const node1Target = B .querySelector (' #node1' )
147- const node2Target = B .querySelector (' #node2' )
134+ const B = A .cloneNode (true );
135+ const node1 = A .querySelector (" #node1" );
136+ const node2 = A .querySelector (" #node2" );
137+ const node1Target = B .querySelector (" #node1" );
138+ const node2Target = B .querySelector (" #node2" );
148139
149- findCorrespondingNode (A , B , node1) // node1Target
150- findCorrespondingNode (A , B , node2) // node2Target
140+ findCorrespondingNode (A , B , node1); // node1Target
141+ findCorrespondingNode (A , B , node2); // node2Target
151142```
152143
153144``` js
154145const findCorrespondingNode = (rootA , rootB , target ) => {
155- if (rootA === target) return rootB;
156-
157- if (rootA .childElementCount ) {
158- for (let i = 0 ; i < rootA .childElementCount ; i++ ) {
159- let result = findCorrespondingNode (rootA .children [i], rootB .children [i], target);
160- if (result) {
161- return result
146+ if (rootA === target) return rootB;
147+
148+ if (rootA .childElementCount ) {
149+ for (let i = 0 ; i < rootA .childElementCount ; i++ ) {
150+ let result = findCorrespondingNode (
151+ rootA .children [i],
152+ rootB .children [i],
153+ target
154+ );
155+ if (result) {
156+ return result;
162157 }
163- }
158+ }
164159 }
165- }
160+ };
166161```
167162
168163###### References
164+
169165- https://bigfrontend.dev/problem/find-corresponding-node-in-two-identical-DOM-tree
170166
171167<br />
172168
173- #### Q6
174- ### Write a function to get depth of a given DOM tree
169+ ### Q. Write a function to get depth of a given DOM tree
175170
176171- A depth of a given DOM tree is the max depth till which DOM nodes are nested
177172
178-
179173``` js
180174/**
181175 * @param {HTMLElement | null} tree
182176 * @return {number}
183177 */
184- function getHeight (root ) {
185- if (! root) return 0 ;
178+ function getHeight (root ) {
179+ if (! root) return 0 ;
186180
187181 let maxDepth = 0 ;
188182
189- const helper = (current , depth = 1 ) => {
190- if (current .hasChildNodes ()) {
191- for (let child of current .children ) {
183+ const helper = (current , depth = 1 ) => {
184+ if (current .hasChildNodes ()) {
185+ for (let child of current .children ) {
192186 helper (child, depth + 1 );
193187 }
194188 }
195- maxDepth = Math .max (maxDepth, depth)
196- }
189+ maxDepth = Math .max (maxDepth, depth);
190+ };
197191
198- helper (root)
199- return maxDepth
192+ helper (root);
193+ return maxDepth;
200194}
201195```
202196
203197<br >
204198
205- #### Q7
206- ### Implement a function to get the root node of a given DOM fragment (document.getRootNode() method)
199+ ### Q. Implement a function to get the root node of a given DOM fragment (document.getRootNode() method)
207200
208201- Root node is the topmost parent node of any given DOM fragment
209202
@@ -212,10 +205,10 @@ function getHeight (root) {
212205 * @param {HTMLElement | null} tree
213206 * @return {HTMLElement | null}
214207 */
215- function getRootNode (tree ) {
216- if (! tree) return null ;
208+ function getRootNode (tree ) {
209+ if (! tree) return null ;
217210
218- while (tree .parentElement ) {
211+ while (tree .parentElement ) {
219212 tree = tree .parentElement ;
220213 }
221214
@@ -224,28 +217,28 @@ function getRootNode (tree) {
224217```
225218
226219###### References
220+
227221- https://javascript.info/dom-navigation
228222
229223<br >
230224
231- #### Q8
232- ### Implement a function to get unique tag names in a given DOM tree
225+ ### Q. Implement a function to get unique tag names in a given DOM tree
233226
234227``` js
235228/**
236229 * @param {HTMLElement | null} tree
237230 * @return {Array}
238231 */
239232function getUniqueTags (root , result = new Set ()) {
240- if (! root) return [];
233+ if (! root) return [];
241234
242- if (! result .has (root .tagName )) {
235+ if (! result .has (root .tagName )) {
243236 result .add (root .tagName );
244237 }
245238
246- if (root .hasChildNodes ()) {
247- for (let child of root .children ) {
248- getUniqueTags (child, result)
239+ if (root .hasChildNodes ()) {
240+ for (let child of root .children ) {
241+ getUniqueTags (child, result);
249242 }
250243 }
251244
@@ -254,12 +247,12 @@ function getUniqueTags(root, result = new Set()) {
254247```
255248
256249###### References
250+
257251- https://bigfrontend.dev/problem/get-DOM-tags
258252
259253<br >
260254
261- #### Q9
262- ### Implement a function to get elements by tag name (document.getElementsByTagName() method)
255+ ### Q. Implement a function to get elements by tag name (document.getElementsByTagName() method)
263256
264257- The getElementsByTagName method of Document interface returns an HTMLCollection of elements with the given tag name.
265258- For example, document.getElementsByTagName('div') returns a collection of all div elements in the document.
@@ -270,17 +263,17 @@ function getUniqueTags(root, result = new Set()) {
270263 * @return {Array}
271264 */
272265function getElementsByTagName (root , tagName ) {
273- if (! root) return [];
266+ if (! root) return [];
274267
275268 let result = [];
276269
277- if (root .tagName .toLowerCase () === tagName .toLowerCase ()) {
270+ if (root .tagName .toLowerCase () === tagName .toLowerCase ()) {
278271 result .push (root);
279272 }
280273
281- if (root .hasChildNodes ()) {
282- for (let child of root .children ) {
283- result = result .concat (getElementsByTagName (child, tagName))
274+ if (root .hasChildNodes ()) {
275+ for (let child of root .children ) {
276+ result = result .concat (getElementsByTagName (child, tagName));
284277 }
285278 }
286279
@@ -289,12 +282,12 @@ function getElementsByTagName(root, tagName) {
289282```
290283
291284###### References
285+
292286- https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByTagName
293287
294288<br >
295289
296- #### Q10
297- ### Implement a function to check if a given DOM tree has duplicate IDs
290+ ### Q. Implement a function to check if a given DOM tree has duplicate IDs
298291
299292- In a given DOM tree, the id on each node has be unique
300293- Although HTML is very forgiving, but we should avoid duplicate identifiers
@@ -305,16 +298,16 @@ function getElementsByTagName(root, tagName) {
305298 * @return {Boolean}
306299 */
307300function hasDuplicateId (tree , idSet = new Set ()) {
308- if (! tree) return false ;
301+ if (! tree) return false ;
309302
310- if (idSet .has (tree .id )) return true ;
303+ if (idSet .has (tree .id )) return true ;
311304
312305 tree .id && idSet .add (tree .id );
313306
314- if (tree .hasChildNodes ()) {
315- for (let child of tree .children ) {
316- const result = hasDuplicateId (child, idSet);
317- if (result) return true ;
307+ if (tree .hasChildNodes ()) {
308+ for (let child of tree .children ) {
309+ const result = hasDuplicateId (child, idSet);
310+ if (result) return true ;
318311 }
319312 }
320313
0 commit comments