Skip to content

Instantly share code, notes, and snippets.

@nitely
Created March 9, 2017 19:38
Show Gist options
  • Select an option

  • Save nitely/d8cd0e2953e3741d22ebfbff6c178029 to your computer and use it in GitHub Desktop.

Select an option

Save nitely/d8cd0e2953e3741d22ebfbff6c178029 to your computer and use it in GitHub Desktop.
Lame JS puzzle #3
/*
This is just a BFS. It only works if the node to find is unique (ie: has an ID)
*/
function findNode(curr, node) {
if (curr == null)
return;
if (curr.isEqualNode(node))
return curr;
for (let n of curr.childNodes) {
let foundNode = findNode(n, node);
if (foundNode != null)
return foundNode;
}
}
let waldo = document.querySelectorAll('.waldo');
let root = document.querySelector('body')
console.log(findNode(root, waldo))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment