# AI solution for JavaScript Coding Question on BFE.dev

17. Create a simple store for DOM element

One solution to create a Node Store that supports DOM elements as keys in older JavaScript environments (such as ES5) is to use an array to store pairs of keys and values. We can use a loop to iterate over the array, checking each key in turn to see if it matches the requested key. If it does, we return the value associated with that key; if not, we continue iterating through the array.

Here's an implementation of the NodeStore class using this approach:

`class NodeStore { constructor() { this.store = []; } set(node, value) { let found = false; for (let i = 0; i < this.store.length; i++) { if (this.store[i][0] === node) { this.store[i][1] = value; found = true; break; } } if (!found) { this.store.push([node, value]); } } get(node) { for (let i = 0; i < this.store.length; i++) { if (this.store[i][0] === node) { return this.store[i][1]; } } return undefined; } has(node) { for (let i = 0; i < this.store.length; i++) { if (this.store[i][0] === node) { return true; } } return false; }}`

The `set`

method first checks if the node already exists in the store, and if so, updates its value. If the node is not found, a new key-value pair is pushed onto the end of the array.

The `get`

method iterates through the array and returns the value associated with the requested node if it is found. Otherwise, it returns `undefined`

.

The `has`

method also iterates through the array, returning `true`

if the requested node is found, and `false`

otherwise.

The time complexity of `set`

and `get`

in this implementation is O(n), where n is the number of key-value pairs in the store (i.e. the length of the array). The worst-case scenario is when the requested node is not found and we need to iterate through the entire array. The space complexity is also O(n), because we need to store each key-value pair in the array.

Note that this implementation assumes that each node is unique, meaning that no two node objects in the DOM are equal. If there are multiple identical node objects in the DOM, they will be treated as separate keys in the store. If this is a concern, you could consider using the `nodeId`

property of DOM elements, which is guaranteed to be unique within a document.