?
Solved

javascript function code improvement

Posted on 2011-10-05
2
Medium Priority
?
263 Views
Last Modified: 2012-05-12
I’m looking to improve a function call that returns a file name based on three args that are passed.

arg data examples of category, rptGroup & rptItem:

category    rptGroup    rpItem      
Flash             g1           RptItem1
Flash             g1           RptItem2
Flash             g3           RptItem7
Demand       g5           RptItem6

category   > 1-m rptGroups
rptGroups > 1-m rptItems

variables store the file names (var naming represents the category (f = flash, d = demand, etc.), group (g1 = group1, etc.) and report item # which is sequential within a category/group):

var fg1RptItem1 = '/flashgroup1rpt1.pdf';    
var fg1RptItem2 = '/ flashgroup1rpt2.pdf';
var fg2RptItem1 = '/flashgroup2rpt1.pdf';  
var dg1RptItem1 = '/demandgroup1rpt1.pdf';    
var dg1RptItem2 = '/demandgroup1rpt2.pdf';    
etc.

Call example:

var category = ‘Flash’;
var rptGroup = ‘g1’;
var rptItem   = ‘RptItem1’;

GetFilename(category,rptGroup,rptItem)

returns:  rptItem =  '/flashgroup1rpt1.pdf'

function GetFilename(category,rptGroup,rptItem)
{
   switch(parentCategory+'|'+rptGroup) {

    case 'Flash|g1' :
    //Return appropriate value based on rptItem
    switch(rptItem) {
       case 'RptItem1' : rptItem = fg1RptItem1; break;
       case 'RptItem2' : rptItem = fg1RptItem2; break;  
    } ; break;

    case 'Flash|g2' :
    switch(rptItem) {
       case 'RptItem1' : rptItem = fg2RptItem1; break;
       case 'RptItem2' : rptItem = fg2RptItem2; break;  
       case 'RptItem3' : rptItem = fg2RptItem3; break;  
    } ; break;

    case 'Demand|g1' :
    switch(rptItem) {
       case 'RptItem1' : rptItem = dg1RptItem1; break;
       case 'RptItem2' : rptItem = dg1RptItem2; break;  
    }; break;
   }
   return rptItem;  //returns report file name i.e. '/flashgroup1rpt1.pdf'
}

Suggestions of an improvement (loop, array, ?) along with a code snippet example based on the above example would be greatly appreciated!

Thank you!
0
Comment
Question by:ssebring
2 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 2000 total points
ID: 36921028
Perhaps this?

DEMO


var myArgs = {
    Flash: {
        g1: {
            RptItem1:"fg1RptItem1",
            RptItem2:"fg1RptItem2"
        },
        g2: {
            RptItem1:"fg1RptItem1",
            RptItem2:"fg1RptItem2",
            RptItem3:"fg1RptItem3"
        }
    },
    Demand :{
        g1: {
            RptItem1:"dg1RptItem1",
            RptItem2:"dg1RptItem2"
        }
    }
}
   

function GetFilename(category,rptGroup,rptItem) {
   return myArgs[category][rptGroup][rptItem]+'.pdf';
}


var category = "Flash";
var rptGroup = "g1";
var rptItem  = "RptItem1";

alert(GetFilename(category,rptGroup,rptItem))

Open in new window

0
 

Author Closing Comment

by:ssebring
ID: 36925174
can't thank you enough!  This is an elegant solution that has opened all sorts of shortcuts in the existing code.  Looks like it will be a great way to go with this.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question