INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How can I simplify this code - shorter or neater

How can I simplify this code - shorter or neater

(OP)
I have a method within which I use

CODE

var prcLevel=$("#orderEntryForm select[name=pricelevel]").val();
        var iPrice=0; var iDisc=0;
        switch(prcLevel) {
            case '0':
                iPrice=varstr.sellprice0;
                iDisc=varstr.disc0;
                break;
            case '1':
                iPrice=varstr.sellprice1;
                iDisc=varstr.disc1;
                break;
            case '2':
                iPrice=varstr.sellprice2;
                iDisc=varstr.disc2;
                break;
            case '3':
                iPrice=varstr.sellprice3;
                iDisc=varstr.disc3;
                break;
        } 
varstr is passed via json and as you can see, I'm referencing four possible variables. I know there's gotta be a way to shrink this.

What would you suggest?

The code I have work, but I figure I look for alternative for educational purpose and in search for simpler coding style.

thanks,

--
SouthBeach
http://www.fp2php.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

RE: How can I simplify this code - shorter or neater

2
Hi

Use the bracket notation :

CODE --> JavaScript

var prcLevel=$("#orderEntryForm select[name=pricelevel]").val();
var iPrice=0; var iDisc=0;

if (prcLevel >= 0 && prcLevel <= 3) {
    iPrice=varstr['sellprice'+prcLevel];
    iDisc=varstr['disc'+prcLevel];
} 

Feherke.
feherke.ga

RE: How can I simplify this code - shorter or neater

(OP)
I tried it with curvy brackets { } - I really did not know you can use these like that, I'm used to use them for arrays that it never occurred to me.

Thanks,

--
SouthBeach
http://www.fp2php.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

RE: How can I simplify this code - shorter or neater

yes, use square brackets in place of dot notation especially when using calculated / dynamic variable names.

So

CODE

var prcLevel = 0;
alert(varstr['sellprice'+prcLevel]);

or

var sName = 'sellprice0';
alert(varstr[sName]);

or 

alert(varstr['sellprice0']); 

are equivalent to

CODE

alert(varstr.sellprice0); 

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

RE: How can I simplify this code - shorter or neater

(OP)
I need more sleep - I just realized it is being referenced as an associative array and the array name (key) is being constructed. lol

--
SouthBeach
http://www.fp2php.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

RE: How can I simplify this code - shorter or neater

(OP)
1DMF, thanks!

That was very sweet of you. It never gets old, one will over think things and miss the simple approach. Of course, getting old and coding in multiple environment gets to you too. lol It is like raising a bunch of kids and they all speak different Languages.

Happy Holidays to you all!

--
SouthBeach
http://www.fp2php.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

RE: How can I simplify this code - shorter or neater

Know that feeling, as I code many languages also, I'm forever having to google the syntax, as it varies from language to language.

I know the concept doesn't change, but can't always fathom the construct ponder

Happy Christmas santa3

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close