JSFiddle - React, Tailwind, and code Playground

by khushboo097

JavaScript

/**
 * Read FAQs section on the left for more information on how to use the editor
 **/
// DO CHANGE FUNCTION NAME

const containsAny = (arr, searchable) => {
  return arr.some((a) => searchable.includes(a));
}
const countElements = (parent, search, nodeList) => {
  if(!parent.children?.length) return;
  const childrenList = Array.from(parent.children);
  childrenList.forEach((child) => {
    if (containsAny(Array.from(child.classList), search)) {
      nodeList.push(child)
    }
    countElements(child, search, nodeList)
  });
}
function getElementsByClassName(search) {
  // write your code below
  let nodeList = []
  let current = this
  //  console.log('in calculating...', current.children)

  countElements(current, search.split(" "), nodeList);
  return nodeList
}

Document.prototype.getByClassName = getElementsByClassName
const div1 = document.createElement("div")
div1.innerHTML = "here in div 1"
div1.classList.add("r")
const div2 = document.createElement("div2")
div2.innerHTML = "here in div 2"
div2.classList.add("blue")
const body = document.getElementsByTagName("body")[0];
body.appendChild(div1);
body.appendChild(div2);
console.log(document.getByClassName("red blue").length)