combining multiple while() loops into one?

J

jquertil

Hello,

I have this snippet of code which works fine, but it seems like I
should be able to combine this into a single while loop, no?

while (fobj.tagName != topelement && fobj.className != "dragme"){
fobj = nn6 ? fobj.parentNode : fobj.parentElement;
}
while (fobj2.tagName != topelement && fobj2.className != "draggerix")
{
fobj2 = nn6 ? fobj2.parentNode : fobj2.parentElement;
}

I tired this version but that broke the function.

while ((fobj.tagName != topelement && fobj.className != "dragme")
&&(fobj2.tagName != topelement && fobj2.className !=
"draggerix")){
fobj2 = nn6 ? fobj2.parentNode : fobj2.parentElement;
fobj = nn6 ? fobj.parentNode : fobj.parentElement;
}
 
D

Doug Gunnoe

Hello,

I have this snippet of code which works fine, but it seems like I
should be able to combine this into a single while loop, no?

  while (fobj.tagName != topelement && fobj.className != "dragme"){
    fobj = nn6 ? fobj.parentNode : fobj.parentElement;
  }
  while (fobj2.tagName != topelement && fobj2.className != "draggerix")
{
    fobj2 = nn6 ? fobj2.parentNode : fobj2.parentElement;
  }

I tired this version but that broke the function.
  while ((fobj.tagName != topelement && fobj.className != "dragme")
       &&(fobj2.tagName != topelement && fobj2.className !=
"draggerix"))

    fobj2 = nn6 ? fobj2.parentNode : fobj2.parentElement;
    fobj = nn6 ? fobj.parentNode : fobj.parentElement;
  }

But what if fobj.tagName != topelement && fobj.className != "dragme"
is true
But fobj2.tagName != topelement && fobj2.className != "draggerix" is
false

don't you still need to do "fobj = nn6 ? fobj.parentNode :
fobj.parentElement;" like happens with the two while loops?

In the 2nd solution that failed, this is skipped.

I would say do:

while ((fobj.tagName != topelement && fobj.className != "dragme")
||(fobj2.tagName != topelement && fobj2.className !=
"draggerix"))

if(fobj.tagName != topelement && fobj.className != "dragme")
fobj = nn6 ? fobj.parentNode : fobj.parentElement;
if(fobj2.tagName != topelement && fobj2.className !=
"draggerix"))
fobj2 = nn6 ? fobj2.parentNode : fobj2.parentElement;

}

But it is hard for me to say that is correct when I don't understand
the relationship between fobj and fobj2.

Good luck.
 
J

jquertil

thanks. i realize the ... )&&( ... should probably be a ...) || (...
then it should be equivalent, right?

am I even gaining any performance this way? I imagine doing several
while loops, one after another is more "expensive" than doing one
complex one, no?

thanks!
 
D

Doug Gunnoe

thanks. i realize the ... )&&( ... should probably be a ...) || (...
then it should be equivalent, right?

It is hard for me to say that it is equivalent because I'm not sure
about the details of what you are doing.

For example, with two while loops

while(condition1) will stop when condition1 is false

while(condition2) will stop when condition2 is false

but this

while(condition1 || condition2) will only stop when both are false at
the same time, such that condition1 could be false at say the 10th
iteration, but then true again at the 15th, as long as between 10 and
15 condition2 is true.

So I can't really say it is equivalent.
am I even gaining any performance this way? I imagine doing several
while loops, one after another is more "expensive" than doing one
complex one, no?

thanks!

If the first loop iterates through the same stuff the second loop
iterates through, then doing two loops is a waste.
 
P

pr

jquertil said:
I have this snippet of code which works fine, but it seems like I
should be able to combine this into a single while loop, no?

I think Doug's on the right track in saying 'no'. Since you're
apparently trying to ascend from (probably) an event target to an
ancestor element having a given class, then doing it for two elements
simultaneously relies on the same number of steps being involved. This
would break things:

fobj -> parent -> parent -> parent -> result
fobj2 -> parent -> result
while (fobj.tagName != topelement && fobj.className != "dragme"){
fobj = nn6 ? fobj.parentNode : fobj.parentElement;

Since you mention performance in another post, why not code a separate
loop for the minority of browsers unable to use parentNode? Checking
'nn6' (questionable variable name, by the way) in a loop inside
(potentially) a mousemove event listener will slow things down considerably.
}
while (fobj2.tagName != topelement && fobj2.className != "draggerix")
{
fobj2 = nn6 ? fobj2.parentNode : fobj2.parentElement;
}

[...]

If you can give more details of what you're trying to do, you could get
more definitive answers about how best to code it.
 
T

Thomas 'PointedEars' Lahn

jquertil said:
I have this snippet of code which works fine, but it seems like I
should be able to combine this into a single while loop, no?

while (fobj.tagName != topelement && fobj.className != "dragme"){
fobj = nn6 ? fobj.parentNode : fobj.parentElement;
}
while (fobj2.tagName != topelement && fobj2.className != "draggerix")
{
fobj2 = nn6 ? fobj2.parentNode : fobj2.parentElement;
}

Maybe you were looking for this:

while (fobj.tagName != topelement
&& !/^(dragme|draggerix)$/i.test(fobj.className))
{
fobj = nn6 ? fobj.parentNode : fobj.parentElement;
}
I tired this version but that broke the function.

It should not have. Obviously you have not posted some important parts of
the code.
while ((fobj.tagName != topelement && fobj.className != "dragme")
&&(fobj2.tagName != topelement && fobj2.className != ^^^^^^^^^^^^^^^^^^^^^^^^^^^
"draggerix")){

The marked operand is redundant as it was already evaluated to `true'
before. fobj === fobj2 here.

[pretty-printed]
fobj2 = nn6 ? fobj2.parentNode : fobj2.parentElement;
fobj = nn6 ? fobj.parentNode : fobj.parentElement;

One statement is redundant.

Your original code did not make sense already, the meaning of your rewrite
is really beyond me.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top