Jump to content

Unusual for loop


roundcorners

Recommended Posts

Can someone explain how this works please?for (var d=fieldChecks.length; d--; ) { initChecks(fieldChecks[d]); }Thank you in advance
I think this is what you're trying to do:for(i=fieldChecks.length;i>0;i--){ initChecks(fieldChecks);}Its a loop that runs till it is true. i decrements till it is equal to or less that 0. After that it ends.Though I am not very clear on what you mean in your post.
Link to comment
Share on other sites

there is a missing conditional in that for loop, and its is needed because eventually it will throw an error once d reaches 0. (because array indexes are always >= 0)anyway, the for loop assigns a variable d to the length of array fieldChecks and after each iteration decrements it by -1. The for loop itself runs a function called initChecks that passes the value of whatever is in fieldCheck[d] at that time for relative to what d equals during that iteration. It basically starts at the end of an array and passes each of its values to a function starting from the end back to the beginning.

Link to comment
Share on other sites

I think this is what you're trying to do:for(i=0;i<fieldChecks.length;i++){ initChecks(fieldChecks);}Its a loop that runs till it is true. i increments till it is equal to or more that fieldChecks length. after that it ends.Though I am not very clear on what you mean in your post.
although it is missing the third conditional, there could be a very good reason why it is starting at the end and working backwards. LIFO comes to mind.
Link to comment
Share on other sites

although it is missing the third conditional, there could be a very good reason why it is starting at the end and working backwards. LIFO comes to mind.
Didn't see the decrement. Let me edit that in.
Link to comment
Share on other sites

Since d is getting decremented with every iteration, eventually it becomes 0. At that point, the loop terminates because d evaluates to false. The third term of the for-loop initializer is optional as long as the ; delimiter is present. This is valid code and it should work just fine.I believe that was the intent of the original question. Not, "How do I fix it", but "This works and I'd like to know why."On the other hand, it is very old-fashioned spacing-saving code such as you might find in a C program circa 1978. There is no advantage to writing code this terse in a JavaScript environment. But there is a disadvantage: what it is supposed to do is not intuitively obvious to many developers. For this reason, I recommend writing it out the long way as in Post #2.

Link to comment
Share on other sites

hmmm...didn't know about the two part for loop conditional or that it in this case it would automatically terminate once it reaches 0. interesting stuff.

Link to comment
Share on other sites

It pays to know exactly what will evaluate to false and under which conditions. This problem is what led to the === operator, which older languages did not have. Sometimes, for clarity, it really pays to write out what you mean, as in:d === 0ord === falseA lot of expressions "work," but six months later you look at your code and say, "Huh?" Even worse when you put a coworker or client through a moment like that. And so unnecessary in environments where all the RAM you want is practically free.

Link to comment
Share on other sites

i think i see it....what confuses me is that when one increments often times it starts with 0, so why would 0 evaluate to false in this situation? for instance:

for(var i = 0, l = myArray.length; i < l; i++){  alert("i is: " + i);}

would print out i (obviously) but would start at 0...but yet wouldn't terminate so....what am I missing?

Link to comment
Share on other sites

For one thing, you're testing a comparison. Each of the following:0 < 1andfalse < 1evaluates as true.The original:(var d = fieldChecks.length; d--; ) // third expression legally omittedtests whether d evaluates to false or not. It's more like an existence test.The decrement operator, obviously, executes after the test, with the final value of d (after the loop) being -1. In fact, one simple (and more intuitive) way of rewriting this thing would be this:(var d = fieldChecks.length; d; d--)The difference being this: the decrement operator does not execute after the test, so the final value of d (after the loop) is 0.Terse code is efficient, but the results are not always obvious.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...