Skip to main content
Known Participant
September 15, 2020
Question

for loop aborting

  • September 15, 2020
  • 2 replies
  • 693 views

I'm running the following code and my for loop is aborting at  *  n=42  *

Can anyone see why?

 

var f = this.getField("cbEdit");
var n = 0
for (var n=16;i<=112;n++){
app.alert(n);
if(n<70 || n>71){
{
s=n+"";
if(f.isBoxChecked(0)){
this.getField("CBR"+s).display = display.visible;
this.getField("CBV"+s).display = display.visible;
this.getField("CBM"+s).display = display.visible;
this.getField("Post"+s).display = display.visible;
}
else {
this.getField("CBR"+s).display = display.hidden;
this.getField("CBV"+s).display = display.hidden;
this.getField("CBM"+s).display = display.hidden;
this.getField("Post"+s).display = display.hidden;
}
}
}
}

This topic has been closed for replies.

2 replies

Karl Heinz  Kremer
Community Expert
Community Expert
September 15, 2020

Just as Bernd said, check the console for errors. It's very likely a problem with a non-existing field. 

 

There are a few things I would change in your script:

 

As Bernd also pointed out, you are mixing variables in your for loop. You initialize and increment "n", but your loop condition  uses "i".

There is an extra set of {} in the loop. 

The expression "if (n < 70 || n > 71)" should probably be rewritten to "if (n != 71)", that's easier to understand than the two combined expressions. 

It is not necessary to convert "n" to "s" by adding an emtpy string. You are doing the conversion from integer to string when you create the field name (e.g. "CBR" + s). If you want to make sure that you have a string, there is an explicit "to string" conversion built into Javascript&colon; You can either use n.toString() or String(n) to force the conversion. 

Hope that helps. 

Known Participant
September 15, 2020

Thanks Karl!

Changed the i to n for testing and missed one.

Where are the {}'s not necessary?

The expression excludes 70 & 71

I see your "n" to "s" point - thanks for the string advice.
Cheers!

Karl Heinz  Kremer
Community Expert
Community Expert
September 15, 2020

Ah now I see that you are excluding two values. 

 

Here is the code with two comments added that point to the curly braces in question: 

 

var f = this.getField("cbEdit");
var n = 0
for (var n = 16; i <= 112; n++) {
	app.alert(n);
	if (n < 70 || n > 71) {
		{ // <-- this is extra
			s = n + "";
			if (f.isBoxChecked(0)) {
				this.getField("CBR" + s).display = display.visible;
				this.getField("CBV" + s).display = display.visible;
				this.getField("CBM" + s).display = display.visible;
				this.getField("Post" + s).display = display.visible;
			} else {
				this.getField("CBR" + s).display = display.hidden;
				this.getField("CBV" + s).display = display.hidden;
				this.getField("CBM" + s).display = display.hidden;
				this.getField("Post" + s).display = display.hidden;
			}
		} // <-- this is extra
	}
}

 

Anytime I look at code, I bring it into my editor and let it format the code. This will make things like that obvious. 

Bernd Alheit
Community Expert
Community Expert
September 15, 2020

Check the Javascript console for errors. The script will stop when a field doesn't exists.

And why does you use this:

 i<=112

 

Known Participant
September 15, 2020

Thanks Bernd!  Yup ... I had a miss in the fields name sequence.  I should have known that without having to post.

Reason:  All fields within the loop value set are modified.