Avatar of ssebring
ssebring

asked on 

javascript function code improvement

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!
Web Languages and StandardsScripting LanguagesHTML

Avatar of undefined
Last Comment
ssebring
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of ssebring
ssebring

ASKER

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.
HTML
HTML

HTML (HyperText Markup Language) is the main markup language for creating web pages and other information to be displayed in a web browser, providing both the structure and content for what is sent from a web server through the use of tags. The current implementation of the HTML specification is HTML5.

62K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo