My solution of Tree Walker

54 views
Skip to first unread message

David Cheung

unread,
Nov 23, 2022, 6:03:41 AM11/23/22
to Intro to JavaScript
nextNode = function(node) {
  //...Code goes here to walk to the next node...
  if (node.firstChild !== null) {
    node = node.firstChild;
  } else if (node.nextSibling !== null) {
    node = node.nextSibling;
  } else {
      var pnode = node.parentNode;
      var found = false;
      while ( pnode !== null) {
        if (pnode.nextSibling !== null) {
          node = pnode.nextSibling;
          found = true;
          break;
        }
        else {
          pnode = pnode.parentNode;
        }
      }
      if (found !== true) { node = null;}
  }
  return node;
}


Reply all
Reply to author
Forward
0 new messages