Coding Challenge Practice – Question 28

The task is to implement a Node Store, which supports DOM elements as keys.

The boilerplate code

class NodeStore {
/**
* @param {Node} node
* @param {any} value
*/
set(node, value) {

}
/**
* @param {Node} node
* @return {an…


This content originally appeared on DEV Community and was authored by Bukunmi Odugbesan

The task is to implement a Node Store, which supports DOM elements as keys.

The boilerplate code

class NodeStore {
   /**
   * @param {Node} node
   * @param {any} value
   */
  set(node, value) {

  }
  /**
   * @param {Node} node
   * @return {any}
   */
  get(node) {

  }

  /**
   * @param {Node} node
   * @return {Boolean}
   */
  has(node) {

  }
}

To use DOM elements as keys, we use a WeakMap. It is designed specifically for this kind of task because its key must be objects, and it automatically removes entries when the key object is no longer in memory, preventing memory leaks.

The first step is to initialize a new WeakMap to store mappings between nodes and values.

this._store = new WeakMap()

The set method adds a new entry to the store. It takes the DOM node and any other value and saves the mapping in the WeakMap.

set(node, value) {
   this._store.set(node, value)
  }

The get method retrieves the value associated with a given node

get(node) {
    return this._store.get(node)
  }

The has method checks whether a particular node exists as a key in the store

has(node) {
   return this._store.has(node)
  }

There's no need to check for invalid inputs because WeakMap automatically adds that.

The final code is:

class NodeStore {
   constructor() {
    this._store = new WeakMap()
   }
  set(node, value) {
   this._store.set(node, value)
  }
  get(node) {
    return this._store.get(node)
  }
  has(node) {
   return this._store.has(node)
  }
}

That's all folks!


This content originally appeared on DEV Community and was authored by Bukunmi Odugbesan


Print Share Comment Cite Upload Translate Updates
APA

Bukunmi Odugbesan | Sciencx (2025-10-15T22:54:37+00:00) Coding Challenge Practice – Question 28. Retrieved from https://www.scien.cx/2025/10/15/coding-challenge-practice-question-28/

MLA
" » Coding Challenge Practice – Question 28." Bukunmi Odugbesan | Sciencx - Wednesday October 15, 2025, https://www.scien.cx/2025/10/15/coding-challenge-practice-question-28/
HARVARD
Bukunmi Odugbesan | Sciencx Wednesday October 15, 2025 » Coding Challenge Practice – Question 28., viewed ,<https://www.scien.cx/2025/10/15/coding-challenge-practice-question-28/>
VANCOUVER
Bukunmi Odugbesan | Sciencx - » Coding Challenge Practice – Question 28. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/15/coding-challenge-practice-question-28/
CHICAGO
" » Coding Challenge Practice – Question 28." Bukunmi Odugbesan | Sciencx - Accessed . https://www.scien.cx/2025/10/15/coding-challenge-practice-question-28/
IEEE
" » Coding Challenge Practice – Question 28." Bukunmi Odugbesan | Sciencx [Online]. Available: https://www.scien.cx/2025/10/15/coding-challenge-practice-question-28/. [Accessed: ]
rf:citation
» Coding Challenge Practice – Question 28 | Bukunmi Odugbesan | Sciencx | https://www.scien.cx/2025/10/15/coding-challenge-practice-question-28/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.