Highlighted

for loop aborting

New Here ,
Sep 15, 2020

Copy link to clipboard

Copied

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;
}
}
}
}

TOPICS
Acrobat SDK and JavaScript

Views

78

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

for loop aborting

New Here ,
Sep 15, 2020

Copy link to clipboard

Copied

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;
}
}
}
}

TOPICS
Acrobat SDK and JavaScript

Views

79

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 15, 2020 0
Adobe Community Professional ,
Sep 15, 2020

Copy link to clipboard

Copied

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

And why does you use this:

 i<=112

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 15, 2020 0
New Here ,
Sep 15, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 15, 2020 0
Adobe Community Professional ,
Sep 15, 2020

Copy link to clipboard

Copied

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. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 15, 2020 0
New Here ,
Sep 15, 2020

Copy link to clipboard

Copied

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!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 15, 2020 0
Adobe Community Professional ,
Sep 15, 2020

Copy link to clipboard

Copied

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. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 15, 2020 0
New Here ,
Sep 15, 2020

Copy link to clipboard

Copied

Got it Karl ... Thanks!!

What editor do you use?  Any good open source you would recommend?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 15, 2020 0
Adobe Community Professional ,
Sep 15, 2020

Copy link to clipboard

Copied

Most of my development is done on a Mac, that's where I use TextMate. On the PC, I use NotePad++ with the JavaScript package JSTool installed. NotePad++ is free. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 15, 2020 0
New Here ,
Sep 15, 2020

Copy link to clipboard

Copied

THX Karl!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 15, 2020 0