This type of tasks is usually a good thing to do when doing some interview preparation as it’s pretty short, doesn’t require a ton of questions and checks several basic skills at the same time.
Given two identical DOM tree structures, A and B, and a node from A, find the corresponding node in B.
And we’re given the node with id node1 we need to be able to have just #root1, #root2 and #node1 to point to the node with id node2.
Few things you can show off doing a task: understanding of prototyping, knowledge of basic Node properties, such as parentNode and childNodes and array methods.
My first idea was to go from top to bottom to find a needed node, collecting a list of indices as I dive deeper in the tree, but then I remembered about parentNode.
So the second idea is to go from a given node to the root node while collecting indices of the current node among its parent’s children. After reaching the top, we can use the resulting path to go through the second root node and find a symmetrical node.
// This function returns a real array of Nodes, so we can use methonds like "indexOf"
If you have a better solution, or any feedback, leave a comment below.