PDA

View Full Version : FOR loop help please


esmith
November 5th, 2009, 10:01 AM
Why does this code return the error "Execution limit exceeded" in reference to the line with the FOR loop? I've checked values of all variables via temporary return statements and confirmed that none are undefined. What am I doing wrong?
var TotalChars = 0;
var EmailLength = Field("e-mail").length;
var EmailInfo = "";

//London phone #s are 18 characters which may be longer than some email addresses
if ((Field("Work phone").indexOf("+") > -1) && (EmailLength < 18)) TotalChars = 18;

//US phone #s are longer than 12-character email addresses
else if (EmailLength <= 12) TotalChars = 13;

else TotalChars = EmailLength;

if (Field("e-mail") != "") {
EmailInfo = Resource("email").content + " " + Field("e-mail");
if (EmailLength < TotalChars) {
var Padding = TotalChars - EmailLength;
}
else Padding = 0;
var LoopCheck = 0;
if (Padding > 0) {
for (var i = 1; i = Padding; i++) {
LoopCheck++;
}
}
}
return Padding + " " + LoopCheck;

esmith
November 6th, 2009, 06:58 AM
Never mind. With the help of a co-worker, I realized that
for (var i = 1; i = Padding; i++)
should be
for (var i = 1; i > Padding; i++)

tobarstep
November 10th, 2009, 08:11 AM
Never mind. With the help of a co-worker, I realized that
for (var i = 1; i = Padding; i++)
should be
for (var i = 1; i > Padding; i++)

I think you want i < Padding. Otherwise your loop will never execute.

esmith
November 10th, 2009, 09:51 AM
I think you want i < Padding. Otherwise your loop will never execute.
I thought the 3 arguments of a FOR loop were essentially (FROM, UNTIL, INCREASE) which would require the "greater than" symbol. Using the "less than" symbol would suggest the arguments are (FROM, WHILE, INCREASE). Is the latter thinking correct?

LesSjo
November 10th, 2009, 10:28 AM
I believe tobarstep is correct in that the loop will never execute. Unless of course the value of "padding" is less than 1, in which case you will have an endless loop because you are incrementing i (it will always be true).

The second statement of the for loop is telling the loop to continue as long as the condition has not been met.

i.e.
(i is 1; continue this loop as long as i is less than the size of the padding; add 1 to i each iteration until the condition is met)

Dan Korn
November 10th, 2009, 11:17 AM
I thought the 3 arguments of a FOR loop were essentially (FROM, UNTIL, INCREASE) which would require the "greater than" symbol. Using the "less than" symbol would suggest the arguments are (FROM, WHILE, INCREASE). Is the latter thinking correct?
No, it's not either. According to the JavaScript reference:
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/for
It's actually:
for ([initial-expression]; [condition]; [final-expression])
statementThe "initial-expression", "condition", and "final expression" parts are all optional, and they can be just about anything. Generally, the final expression is a simple increment (++) operation, but it can also be a decrement (--) operation, or anything else.

That said, in this particular case, you are indeed doing an increment, or "increase", in the final expression, so Lester is correct that you want to use a less-than operator (<) in the condition.

You also shouldn't need to gate the entire thing with "if (Padding > 0)", because if it isn't greater than zero, then the condition in "for (var i = 1; i > Padding; i++)" will never be true, and the loop statement will not execute.

LesSjo
November 10th, 2009, 11:25 AM
Correct. In the case where the arguments are not used they may be stated outside of the loop or included in the body of the loop where the programmer may have additional loop statements added.

Some examples may be this...i = 0;
for (; i < 10;) {
i++;
}


or this...
i = 0;
for (;; i++) {
if (i >= 10)
break;
}

Dan Korn
November 10th, 2009, 11:48 AM
Sure, it's also not uncommon to see this kind of notation:
for (;;)used as a substitute for:
while (true)where obviously the loop body would contain a break or return statement, or some other way of preventing an infinite loop condition (which is reported as an "Execution limit exceeded" error by JavaScript).