Jump to content

FOR loop help please


esmith

Recommended Posts

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;

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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])
  statement

The "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.

Link to comment
Share on other sites

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;

[left][font=monospace] for (; i < 10;) {
   i++;
}[/font][/left]

or this...

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

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...