Objective

Redefine the replaceChild() method using other methods of the Node interface.

Difficulty

Easy

Description

As we learnt in the previous HTML DOM — The Node Interface chapter, the Node interface's replaceChild() method allows us to replace a given node with another node in a document.

The new node is provided as the first argument to the method whereas the previous node is provided as the second argument. As the name suggests, the method itself is called on a node whose child we want to replace.

In the same chapter, we also learnt about the insertBefore() and the removeChild() method.

In quick words, insertBefore() allows us to insert a given node right before another node while removeChild() allows us to remove a given node.

In this exercise, you have to redefine replaceChild() solely in terms of insertBefore() and removeChild().

In a real program, there is absolutely no need of this redefinition — we're better off with the native implementation of the method. The purpose of this exercise is only to refine your knowledge about the Node interface.