Jump to content

For Loop Trouble


Recommended Posts

I need some help with this. Im trying to write a rule that hides pages based on a selection from a drop down menu. The rule validates as "Expressions OK" but when I go to preview it doesn't work. I am able to get it to work if i write it out the long way with out the for loop I was just hoping to avoid the extra typing.

 

var pages = ["Cert 1","Cert 2","Cert 3","Cert 4","Cert 5"]

 

if(Field("Choose A Certificate")=="NSU Certificates.jpg")

{for (i=1; i<pages.length; i++);

{FusionPro.Composition.SetBodyPageUsage(, false)}

};

Link to comment
Share on other sites

You need to reference the name of the array ("pages") when you want to call out a value by its position which I'm assuming you're trying to do in "SetBodyPageUsage." So, that line should be changed to this:

{FusionPro.Composition.SetBodyPageUsage(pages[i], false)}

I'm not sure if it's intentional or not, but you're only turning off 4 pages (Cert 2 - 5) since you're setting 'i' equal to 1 rather than 0. If you want to turn off all pages in the array you need to modify the code to this:

var pages = ["Cert 1","Cert 2","Cert 3","Cert 4","Cert 5"];

if(Field("Choose A Certificate")=="NSU Certificates.jpg"){
for (var i=0; i<pages.length; i++){
	FusionPro.Composition.SetBodyPageUsage(pages[i], false);
}
}

 

You could also do it without the array at all:

if(Field("Choose A Certificate")=="NSU Certificates.jpg"){
for (var i=1; i<=5; i++){
	FusionPro.Composition.SetBodyPageUsage("Cert " + i, false);
}
}

Link to comment
Share on other sites

You need to reference the name of the array ("pages") when you want to call out a value by its position which I'm assuming you're trying to do in "SetBodyPageUsage." So, that line should be changed to this:

{FusionPro.Composition.SetBodyPageUsage(pages[i], false)}

True, although, in the original post, you would also need to remove the semicolon from the end of this line:

{for (i=1; i<pages.length; i++);

Otherwise, the loop isn't going to do anything at all.

I'm not sure if it's intentional or not, but you're only turning off 4 pages (Cert 2 - 5) since you're setting 'i' equal to 1 rather than 0. If you want to turn off all pages in the array you need to modify the code to this:

var pages = ["Cert 1","Cert 2","Cert 3","Cert 4","Cert 5"];

if(Field("Choose A Certificate")=="NSU Certificates.jpg"){
for (var i=0; i<pages.length; i++){
	FusionPro.Composition.SetBodyPageUsage(pages[i], false);
}
}

Right, array indices in JavaScript start at zero, not one. Although, instead of initializing a numeric index, and trying to specify the upper and lower bounds correctly (which is easy to get wrong), it's more reliable to use the for...in syntax instead:

	for (var i in pages)
	FusionPro.Composition.SetBodyPageUsage(pages[i], false);

You could also do it without the array at all:

if(Field("Choose A Certificate")=="NSU Certificates.jpg"){
for (var i=1; i<=5; i++){
	FusionPro.Composition.SetBodyPageUsage("Cert " + i, false);
}
}

True, but if these are the first five pages of the template, you can also do it purely by page number:

if (Field("Choose A Certificate") == "NSU Certificates.jpg")
{
for (var i=1; i<=5; i++)
	FusionPro.Composition.SetBodyPageUsage(i, false);
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...