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.

Post History

33%
+1 −4
Q&A How this recursive treewalker works?

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", "");...

3 answers  ·  posted 2y ago by deleted user  ·  last activity 2y ago by hkotsubo‭

#7: Post edited by (deleted user) · 2022-04-29T10:17:45Z (almost 2 years ago)
  • Credit for [User:Meriton](https://software.codidact.com/users/53280) for developing the following code ([first published here](https://software.codidact.com/posts/286304/286317#answer-286317)):
  • ```javascript
  • 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`?
  • Credit for [User:Meriton](https://software.codidact.com/users/53280) for developing the following code ([first published here](https://software.codidact.com/posts/286304/286317#answer-286317)).
  • ```javascript
  • 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`?
#6: Post edited by user avatar Alexei‭ · 2022-04-29T06:09:45Z (almost 2 years ago)
added relevant tags
#5: Post edited by (deleted user) · 2022-04-28T22:16:03Z (almost 2 years ago)
  • Treewalker with just an if, no else
  • How this recursive treewalker works?
  • Credit for [User:Meriton](https://software.codidact.com/users/53280) for developing the following code ([first published here](https://software.codidact.com/posts/286304/286317#answer-286317)):
  • ```javascript
  • 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);
  • ```
  • Can this be achieved without `else`?
  • If we just want to run on all characters in the document, why not do that in a direct command without conditioning?
  • Credit for [User:Meriton](https://software.codidact.com/users/53280) for developing the following code ([first published here](https://software.codidact.com/posts/286304/286317#answer-286317)):
  • ```javascript
  • 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`?
#4: Post edited by (deleted user) · 2022-04-28T21:57:31Z (almost 2 years ago)
  • Treewalker without just an if, no else
  • Treewalker with just an if, no else
#3: Post edited by (deleted user) · 2022-04-28T21:54:10Z (almost 2 years ago)
  • Treewalker without an if-else conditioning
  • Treewalker without just an if, no else
  • Credit for [User:Meriton](https://software.codidact.com/users/53280) for developing the following code ([first published here](https://software.codidact.com/posts/286304/286317#answer-286317)):
  • ```javascript
  • 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);
  • ```
  • Why does an if-else pattern is needed here and can the code be rephrased without it?
  • If we just want to run on all characters in the document, why not do that in a direct command without conditioning?
  • Credit for [User:Meriton](https://software.codidact.com/users/53280) for developing the following code ([first published here](https://software.codidact.com/posts/286304/286317#answer-286317)):
  • ```javascript
  • 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);
  • ```
  • Can this be achieved without `else`?
  • If we just want to run on all characters in the document, why not do that in a direct command without conditioning?
#2: Post edited by (deleted user) · 2022-04-28T20:17:43Z (almost 2 years ago)
  • Credit for [User:Meriton](https://software.codidact.com/users/53280) for developing the following code ([first published here](https://software.codidact.com/posts/286304/286317#answer-286317)):
  • ```javascript
  • 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);
  • ```
  • Why does an if-else pattern is needed here and can the code be rephrased without it?
  • Credit for [User:Meriton](https://software.codidact.com/users/53280) for developing the following code ([first published here](https://software.codidact.com/posts/286304/286317#answer-286317)):
  • ```javascript
  • 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);
  • ```
  • Why does an if-else pattern is needed here and can the code be rephrased without it?
  • If we just want to run on all characters in the document, why not do that in a direct command without conditioning?
#1: Initial revision by (deleted user) · 2022-04-28T19:58:30Z (almost 2 years ago)
Treewalker without an if-else conditioning
Credit for [User:Meriton](https://software.codidact.com/users/53280) for developing the following code ([first published here](https://software.codidact.com/posts/286304/286317#answer-286317)):

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

Why does an if-else pattern is needed here and can the code be rephrased without it?