Skip to content

Commit e988035

Browse files
committed
Implement NodeStore
1 parent c17e8d0 commit e988035

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

challenges/DOM.md

+47
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
1. [Show the different ways of selecting an element from DOM](#Q1)
88
1. [Show the ways to loop over the Nodelist obtained after querying for the elements](#Q2)
9+
3. [Design and Implement a Node Store, which supports DOM element as key](#Q3)
910

1011
---
1112

@@ -43,4 +44,50 @@ Note goes here
4344

4445
<br />
4546

47+
48+
#### Q3
49+
### Design and Implement a Node Store, which supports DOM element as key
50+
51+
- Implement it without using inbuilt Map
52+
- Can you do it in O(1) Time complexity?
53+
54+
```js
55+
class NodeStore {
56+
57+
constructor() {
58+
this.store = {};
59+
}
60+
/**
61+
* @param {Node} node
62+
* @param {any} value
63+
*/
64+
set(node, value) {
65+
node.__nodeIdentifier__ = Symbol();
66+
this.store[node.__nodeIdentifier__] = value
67+
}
68+
/**
69+
* @param {Node} node
70+
* @return {any}
71+
*/
72+
get(node) {
73+
return this.store[node.__nodeIdentifier__];
74+
}
75+
76+
/**
77+
* @param {Node} node
78+
* @return {Boolean}
79+
*/
80+
has(node) {
81+
return !!this.store[node.__nodeIdentifier__];
82+
}
83+
}
84+
```
85+
86+
###### References
87+
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
88+
89+
<br />
90+
91+
92+
4693
[[] Back to top](#home)

0 commit comments

Comments
 (0)