logo
logo

Get Tag Count

Get Tag Count

Create a function
getTagCount(tagName)
that accepts a
tagName
parameter and returns the number of elements on the page with that tag name. The function should use the
getElementsByTagName()
method to retrieve a collection of elements with the specified tag name.

Example usage:

// Given the following HTML:
// <div>Some content here</div>
// <div>Another div element</div>
// <span>A span element</span>
// <p>A paragraph element</p>

const divCount = getTagCount('div'); // divCount equals 2
const spanCount = getTagCount('span'); // spanCount equals 1
const pCount = getTagCount('p'); // pCount equals 1

Console
Submit
Solution
00:00