Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on How to make this treewalker code having a regular for loop or a forEach() method?

Parent

How to make this treewalker code having a regular for loop or a forEach() method?

+0
−3

Credit for User:Meriton for developing the following code (first published here).

function replaceIn(e) {
  if (e.nodeType == Node.TEXT_NODE) {
    e.nodeValue = e.nodeValue.replaceAll("a", "");
  } else {
    for (const child of e.childNodes) {
      replaceIn(child);
    }
  }
}

replaceIn(document.body);

I don't want to use a for...of loop here by principle, because I personally think its syntax is confusing.

How to make this tree walker having a regular for loop instead, or, alternatively, a forEach() method instead?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+4
−0

I personally think its syntax is confusing.

Well, it's just a matter of getting used to it, I guess :-)


Anyway, this code is just looping through all childNodes and in each iteration it calls the replaceIn function, passing the element as argument.

Therefore, you could replace the for..of with a traditional for loop:

function replaceIn(e) {
  if (e.nodeType == Node.TEXT_NODE) {
    e.nodeValue = e.nodeValue.replaceAll("a", "");
  } else {
    for (let i = 0; i < e.childNodes.length; i++) {
      replaceIn(e.childNodes[i]);
    }
  }
}

Or you could use the forEach method:

function replaceIn(e) {
  if (e.nodeType == Node.TEXT_NODE) {
    e.nodeValue = e.nodeValue.replaceAll("a", "");
  } else {
    e.childNodes.forEach(replaceIn);
  }
}

With both, you achieve the same result.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

2 comment threads

A question (6 comments)
Comments (6 comments)
A question
deleted user wrote over 2 years ago

Would it be correct to define the code snippets in the answer as "pseudo-TreeWaler" due to acting like a JavaScript TreeWalker JavaScript data object?

elgonzo‭ wrote over 2 years ago · edited over 2 years ago

Depends. Is a passenger car a "pseudo motobike" to you (or vice versa, is a motobike a "pseudo car")?

If you see a car as a "pseudo motobike" (or vice versa), then you probably also see this recursive function as some "pseudo TreeWalker". If however you dont' see a car as a "pseudo motobike" (or vice versa), then chances are you also don't see this recursive function as a "pseudo TreeWalker".

In the end it all comes down to how strictly you stick to the dictionary definition of the term (which might vary slightly from language/culture to language/culture in which the term is being used), or how much you want to imbue the term with your own personal invention of a meaning. If we pedantically go with the term as defined by Merriam-Webster, then no, this recursive function is not a "pseudo TreeWalker".

deleted user wrote over 2 years ago

elgonzo‭ no I don't think this analogy helps. Perhaps a car and a jeep would be a better analogy. Anyway, this is a terminology question an answer on which might ease discourse.

hkotsubo‭ wrote over 2 years ago

The code is traversing the DOM tree, just like I imagine a tree-walker would do. I would say it's an actual tree walker, then :-)

elgonzo‭ wrote over 2 years ago · edited over 2 years ago

The analogy could help, if you would just think about how you used the term "pseudo".

Both cars and motobikes are road vehicles, being subject to the very same road traffic rules/laws. Both are wheeled vehicles with the same principle of locomotion (ICE or electric engine acting upon the wheel(s)). Both move people (and luggage/cargo/pizza) from point A to point B, or are used for joy rides. They act pretty much the same way.

You can make totally analogous comparisons between this recursive function and the Javascript TreeWalker API/object that leads --and did lead-- you to conclude that the recursive function acts like Javascript's TreeWalker object. But how would you end up with calling it "pseudo"? If you were to choose calling this recursive function a "pseudo treewalker" then (assuming you are consistent) by the same logic you would also choose to call a car a "pseudo motobike".

Thus, thoughtfully: Is a passenger car a "pseudo motobike" to you (or vice versa)?

deleted user wrote over 2 years ago

elgonzo‭ at least one difference between generally any car to any motorbike is 4 wheels instead two; how about glove compartment and air condition? For me it prevents this particular yet general comparison, but of course, I understand what you meant to say; both are vehicles with many shared uses.