FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Identical DOM Trees

25mins

Implement a function identicalDomTrees(node1, node2) that determines whether two DOM trees are identical.

The function should:

  • Compare the tag names of nodes.
  • Compare text content if nodes are text nodes.
  • Compare children recursively in order.
  • Return true if the entire structures are the same, otherwise false.

Example

const doc1 = new DOMParser().parseFromString( '<div><span>Hi</span><p>Bye</p></div>', 'text/html' ); const doc2 = new DOMParser().parseFromString( '<div><span>Hi</span><p>Bye</p></div>', 'text/html' ); identicalDomTrees(doc1.body, doc2.body); // true const doc3 = new DOMParser().parseFromString( '<div><span>Hello</span><p>Bye</p></div>', 'text/html' ); identicalDomTrees(doc1.body, doc3.body); // false

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests