ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

HTML Element Count

HTML

Goal -- WPM

Ready
Exercise Algorithm Area
1function countElementsByTagName(doc, tagName) {
2let count = 0;
3const elements = doc.getElementsByTagName(tagName);
4
5if (!elements) {
6return 0;
7}
8
9// Loop through all found elements
10for (let i = 0; i < elements.length; i++) {
11count++;
12}
13
14return count;
15}
16
17function getDocumentBody(htmlString) {
18const parser = new DOMParser();
19const doc = parser.parseFromString(htmlString, 'text/html');
20return doc;
21}
22
23function processHtmlForTagCount(htmlContent, targetTag) {
24if (!htmlContent || typeof htmlContent !== 'string') {
25console.error('Invalid HTML content provided.');
26return -1; // Indicate error
27}
28if (!targetTag || typeof targetTag !== 'string' || targetTag.length === 0) {
29console.error('Invalid target tag name provided.');
30return -1; // Indicate error
31}
32
33const doc = getDocumentBody(htmlContent);
34const count = countElementsByTagName(doc, targetTag);
35
36return count;
37}
38
39// Example Usage:
40// const html = '<html><body><h1>Title</h1><p>Paragraph 1</p><p>Paragraph 2</p></body></html>';
41// const h1Count = processHtmlForTagCount(html, 'h1');
42// console.log('H1 count:', h1Count); // Expected: 1
43// const pCount = processHtmlForTagCount(html, 'p');
44// console.log('P count:', pCount); // Expected: 2
45// const divCount = processHtmlForTagCount(html, 'div');
46// console.log('Div count:', divCount); // Expected: 0
Algorithm description viewbox

HTML Element Count

Algorithm description:

This algorithm counts the occurrences of a specific HTML tag within a given HTML document string. It parses the HTML content into a document object and then utilizes the browser's built-in `getElementsByTagName` method to find all matching elements. This is useful for web scraping, content analysis, or validating HTML structure.

Algorithm explanation:

The `processHtmlForTagCount` function first validates its inputs to ensure valid HTML content and a non-empty tag name are provided. It then uses `DOMParser` to convert the HTML string into a manipulable `Document` object. The core logic resides in `countElementsByTagName`, which calls `doc.getElementsByTagName(tagName)`. This method returns an `HTMLCollection` of all elements with the specified tag name. The function then iterates through this collection, incrementing a counter for each element found. The time complexity is dominated by `getElementsByTagName`, which is typically O(N) where N is the total number of nodes in the DOM tree, as it needs to traverse the tree. The space complexity is O(M) in the worst case, where M is the number of matching elements, to store the `HTMLCollection`. Edge cases handled include invalid input strings and the absence of the target tag, returning 0 in the latter case.

Pseudocode:

function processHtmlForTagCount(htmlString, tagName):
  if htmlString is invalid or tagName is invalid:
    return error indicator
  parse htmlString into a document object
  get all elements with tagName from the document
  initialize count to 0
  for each element found:
    increment count
  return count