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?

Parent

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 attention from curators or moderators?
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)
Post
+5
−0

Note: This answer was written for a version of the question that focused on getting rid of the if-else, rather than on an explanation of the workings of the recursive tree-traversal.

One way of getting rid of if-elses to achieve uniform handling of different objects is to hide the distinction in lower levels of abstraction:

Imagine a bowl of apples and bananas. Basically, you just want to eat all of them. However, if you one by one pick the bowl contents, you first have to check if you have a banana - which you need to peel before you bite into it. That is, your algorithm is "for each fruit: if banana, peel then eat, if apple, eat". You can abstract it by introducing a function "prepare_for_eating". Then your algorithm is "for each fruit: prepare_for_eating(fruit) then eat(fruit)". Seems as if the if-else has disappeared. But, if you look inside the "prepare_for_eating" function, you will again have the original if-else statement where you decide to peel or not to peel.

With object oriented techniques you can hide if-elses behind method calls: A Fruit base class would provide a member function "prepare_for_eating". For banana, this would contain the "peel" statement, for the apple, this would be empty. The algorithm would be "for each fruit: fruit.prepare_for_eating then fruit.eat". If fruit happens to be a banana, the banana member function doing the peel would be called. For an apple object, the apple member function would be called that does nothing.

You can use other tricks to get rid of if-elses by computing boolean expressions and use their result as zero or one in multiplications or table lookups. For example:

if x == 42 then x = -1

can be re-written as (assuming true can be used as 1 and false as 0):

x = (x == 42) * -1 + (x != 42) * x;

Or, using such a result for table lookups:

action_table = [peel, do_nothing]
for each fruit: call action_table[fruit is apple] then eat;

Whether any of the above ideas can be applied meaningfully to your problem and is suited to fulfill your expectations is up to you.

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 (3 comments)
Hello ! I understood everything in the answer besides this: x = (x == 42) * -1 + (x != 42) * x;... (8 comments)
Hello ! I understood everything in the answer besides this: x = (x == 42) * -1 + (x != 42) * x;...
deleted user wrote over 2 years ago · edited over 2 years ago

Hello !

I understood everything in the answer besides this:

x = (x == 42) * -1 + (x != 42) * x;

We put in x the result of -42 and then, given that x is not equal 42 we multiply -42 in itself to get +84?

deleted user wrote over 2 years ago

Furthermore when I run x = (x == 42) * -1 + (x != 42) * x; in console I get:

Uncaught ReferenceError: x is not defined at :1:1

hkotsubo‭ wrote over 2 years ago

deleted user You must assign a value to x before running that:

x = 42;
x = (x == 42) * -1 + (x != 42) * x;
deleted user wrote over 2 years ago

hkotsubo‭ please help me to break it down to two steps a moment

x = 42;
x = (x == 42) * -1

Console result:

-1

Why -1 and not -42?

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

deleted user Note that (x == 42) is a comparison (it's checking if x is equals to 42), so the result is a boolean (it can be true or false).

In this case, the result is true. But when you multiply a boolean and a number, the boolean is automatically converted to a number (JavaScript does a lot of those automatic conversions - the fancy name is "type coercion"). And the boolean true, when converted to a number, results in 1, so the code is multiplying 1 by -1, hence the result is -1

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

hkotsubo‭ I think I get it,

Since true was translated into a number 1 and we multiply it by -1 we get -1.

That said, x !=42 is false that would give us zero so -1 + 0 * x should give us -42, right?

Skipping 1 deleted comment.

deleted user wrote over 2 years ago

hkotsubo‭ I don't know why you didn't reply to me here, perhaps you were baffled by some basic math knowledge I lack (this may well be the case as sadly I didn't learn much math in my past but I am always eager to fill in gaps if there are).

For community members in general, I have asked a specified question about this and hkotsubo replied there:

https://software.codidact.com/posts/286372

hkotsubo‭ wrote over 2 years ago

@kalispero Sorry, I had too many unread notifications and probably missed this one