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 this recursive treewalker works?

Post

How this recursive treewalker works?

+1
−4

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);

How this recursive treewalker works?

As a side question which I grasp as important, can there be an even simpler and more direct version without the else?

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

2 comment threads

As explained [here](https://software.codidact.com/posts/286333), there are different types of nodes. ... (8 comments)
What is the goal that you want to achieve? Why do you want to avoid using an if-else? (6 comments)
What is the goal that you want to achieve? Why do you want to avoid using an if-else?
Dirk Herrmann‭ wrote almost 2 years ago

What is the goal that you want to achieve? Why do you want to avoid using an if-else?

deleted user wrote almost 2 years ago · edited almost 2 years ago

Dirk Herrmann‭

The goal I want to achieve is an even simpler code for the exact same purpose (if it's possible) because I misunderstand the current code so perhaps there is a step back by a simpler code.

If it's not possible than I would have to expand my knowledge on for...of loops and why would such a loop come in an else for a pretty much straight forward code (change X in Y).

hkotsubo‭ wrote almost 2 years ago · edited almost 2 years ago

Perhaps you could rephrase the question to "how this recursive function works", because that seems to be the main issue (once you understand this, the else and for become just minor details, IMO)

deleted user wrote almost 2 years ago · edited almost 2 years ago

hkotsubo‭ thanks, I've rephrased the question and made the else issue minor instead of major (it is still important for me for general know to knowledge if one can get rid of this specific else).

Dirk Herrmann‭ wrote almost 2 years ago

Just for my understanding: Are you in principle familiar with the concept of recursion and you only want to understand why in this case it works with the given code? Or is the concept of recursion as such new to you?

deleted user wrote almost 2 years ago

Dirk Herrmann‭ the concept of recursion isn't new to me, I know it from Bash such as when changing permissions to a directory and all directories in it of any level; or, from JavaScript as a function which calls itself.

My problem here is with the syntax of this particular code which I find a bit confusing; perhaps it could be simplified; the main or only thing which interrupts me to understand the code is this very particular else statement; for me, it's not as clear as:

if (myValue = x) {
     do y;
} else {
 do z;
}

I prefer to try to do the same thing just without the particular else I mention.