Go Back   MarcomCentral (PTI) and FusionPro User Communities > Software-Related Talk > The JavaScript Library

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old May 28th, 2021, 06:52 AM
sschardan sschardan is offline
Regular Contributor
 
Join Date: Oct 2008
Location: St. Louis, MO
Posts: 100
Default Making this more concise and dynamic

I just had a job that was a coupon where each card had a unique code, and they wanted the job delivered in multiple sets of differing quantities. Here is the code I used to successfully produce the job:

Code:
// 4 sets of 380

if ((CurrentRecordNumber() >= 1) && (CurrentRecordNumber() <= 1520))

    {   
        SetQuantity = 380
    }
    
// 9 sets of 350

else if ((CurrentRecordNumber() >= 1521) && (CurrentRecordNumber() <= 4670))

    {   
        SetQuantity = 350
    }
    
// 34 sets of 255

else if ((CurrentRecordNumber() >= 4671) && (CurrentRecordNumber() <= 13340))

    {   
        SetQuantity = 255
    }
    
// 26 sets of 215

else if ((CurrentRecordNumber() >= 13341) && (CurrentRecordNumber() <= 18930))

    {   
        SetQuantity = 215
    }
    
// 28 sets of 170

else if ((CurrentRecordNumber() >= 18931) && (CurrentRecordNumber() <= 23690))

    {   
        SetQuantity = 170
    }
    
// 11 sets of 115

else if ((CurrentRecordNumber() >= 23691) && (CurrentRecordNumber() <= 24955))

    {   
        SetQuantity = 115
    }
    
// 1 set of 45

else if ((CurrentRecordNumber() >= 24956) && (CurrentRecordNumber() <= 25000))

    {   
        SetQuantity = 45
    }
    
if (SetCounter == 1)

    {
        FusionPro.Composition.SetBodyPageUsage('divider', true)
    }

if (SetCounter < SetQuantity)
    {
        SetCounter++;
    }
else
    {
        SetCounter = 1;
    }
In javascript globals I have defined SetCounter to 1 and SetQuantity to 0.

Is there a way to make this more concise, and to determine the record numbers in a more dynamic way? I see this as being a repeating job in the future, but with differing set quantities and amounts per set. It would be nice to just enter the number of sets and quantities per set and have code to figure out the record numbers.

I would appreciate any guidance anyone is willing to share!

Thank you...
Reply With Quote
  #2  
Old May 28th, 2021, 02:11 PM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,591
Default Re: Making this more concise and dynamic

Great question! Sure, this logic can be reduced in several ways.

First, since you're in an "if...else" chain, you know that each block is gated by the one before. For instance, if the number is less than 1521, you'll go to the first case, so that next "if" doesn't need to re-check that it's less that 1521 again, and so on. And you don't need to check that the number is at least one at the start; it always is, by definition. You can also get rid of those braces since each statement block is just a single statement. Also, you probably want a final "else" at the end, just in case. So, skipping the "SetCounter" logic at the end, the first reduction is:
Code:
// 4 sets of 380
if (CurrentRecordNumber() <= 1520)
         SetQuantity = 380
// 9 sets of 350
else if (CurrentRecordNumber() <= 4670)
        SetQuantity = 350
// 34 sets of 255
else if (CurrentRecordNumber() <= 13340)
        SetQuantity = 255
// 26 sets of 215
else if (CurrentRecordNumber() <= 18930)
        SetQuantity = 215
// 28 sets of 170
else if (CurrentRecordNumber() <= 23690)
        SetQuantity = 170
// 11 sets of 115
else if (CurrentRecordNumber() <= 24955)
        SetQuantity = 115
// 1 set of 45
else if (CurrentRecordNumber() <= 25000)
        SetQuantity = 45
else
        SetQuantity = 30
The next thing I would do is set this logic up for debugging. This means that you want to be able to test various input values to make sure that the output value is correct. I would do something like this:
Code:
var recNum = CurrentRecordNumber();

recNum = 5000; // debugging

// 4 sets of 380
if (recNum <= 1520)
         SetQuantity = 380
// 9 sets of 350
else if (recNum <= 4670)
        SetQuantity = 350
// 34 sets of 255
else if (recNum <= 13340)
        SetQuantity = 255
// 26 sets of 215
else if (recNum <= 18930)
        SetQuantity = 215
// 28 sets of 170
else if (recNum <= 23690)
        SetQuantity = 170
// 11 sets of 115
else if (recNum <= 24955)
        SetQuantity = 115
// 1 set of 45
else if (recNum <= 25000)
        SetQuantity = 45
else
        SetQuantity = 30

return [recNum, SetQuantity]; // debugging
Now, you can set the number to whatever you want on line 3, and click Validate to see if you get the result you want, right in the Rule Editor, without having to compose. Once you get the logic right, you can just comment out that line that sets the number, and the return line that validates it (with the "debugging" comments).

Now we can try other ways of reducing this code. There are probably different schools of thought about this, but here's where I would land:
Code:
var recNum = CurrentRecordNumber();

//recNum = 20000; // debugging

var Quantities = 
{
    1520: 380, // 4 sets of 380
    4670: 350, // 9 sets of 350
    13340: 255, // 34 sets of 255
    18930: 215, // 26 sets of 215
    23690: 170, // 28 sets of 170
    24955: 115, // 11 sets of 115
    25000: 45, // 1 set of 45
    0: 30 // default
};

for (var q in Quantities)
{
    if (recNum <= q || q == 0)
    {
        SetQuantity = Quantities[q];
        break;
    }
}

//return [recNum, SetQuantity]; // debugging
Basically, this abstracts the logic of "if x is less than or equal to y, return z" for different values of y and z. Now, you can see all the values in one little table, and it's easy to change them without modifying multiple lines of code. This is an example of the programming principle called DRY, for "Don't Repeat Yourself," basically, try to have the logic written in one definitive place.
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});

Last edited by Dan Korn; May 28th, 2021 at 02:22 PM..
Reply With Quote
  #3  
Old June 1st, 2021, 06:31 AM
sschardan sschardan is offline
Regular Contributor
 
Join Date: Oct 2008
Location: St. Louis, MO
Posts: 100
Default Re: Making this more concise and dynamic

As always Dan, thank you! Your insights and knowledge are immensely appreciated.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 01:10 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.