Search⌘ K
AI Features

Coding Challenge: Showing a Node's Child

Explore how to write a JavaScript function that displays a child element of a DOM node by index. Learn to handle invalid inputs and navigate the DOM structure effectively using node.children to access child elements.

We'll cover the following...

Problem statement

Your mission here is to create a showChild() function that shows one of the children of a DOM element node. This function takes as a parameter the parent node and the child node index. Error cases like a non-element node or an out-of-limits index must be taken into account.

Note: You can fetch all children of a node by using node.children where node is a DOM object.

Coding exercise

Complete the following program to obtain the expected results. The associated HTML code is given in the HTML tab.