Skip to content

Instantly share code, notes, and snippets.

@basekays
Last active January 30, 2017 05:46
Show Gist options
  • Select an option

  • Save basekays/57c5fd6655c5c86ef63d5b776d6ac305 to your computer and use it in GitHub Desktop.

Select an option

Save basekays/57c5fd6655c5c86ef63d5b776d6ac305 to your computer and use it in GitHub Desktop.
recursion.js
var getElementsByClassName = function(className, n) {
// your code here
var resultArr = [];
var searchClass = function(element) {
if (element.classList) {
if (element.classList.contains(className)) {
resultArr.push(element);
}
}
for (var i = 0; i < element.childNodes.length; i++) {
searchClass(element.childNodes[i]);
}
}
searchClass(document.body);
return resultArr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment