Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I simplify this code - shorter or neater 3

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
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
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Hi

Use the bracket notation :
JavaScript:
[b]var[/b] prcLevel[teal]=[/teal]$[teal]([/teal][i][green]"#orderEntryForm select[name=pricelevel]"[/green][/i][teal]).[/teal][COLOR=orange]val[/color][teal]();[/teal]
[b]var[/b] iPrice[teal]=[/teal][purple]0[/purple][teal];[/teal] [b]var[/b] iDisc[teal]=[/teal][purple]0[/purple][teal];[/teal]

[b]if[/b] [teal]([/teal]prcLevel [teal]>=[/teal] [purple]0[/purple] [teal]&&[/teal] prcLevel [teal]<=[/teal] [purple]3[/purple][teal]) {[/teal]
    iPrice[teal]=[/teal]varstr[teal][[/teal][i][green]'sellprice'[/green][/i][teal]+[/teal]prcLevel[teal]];[/teal]
    iDisc[teal]=[/teal]varstr[teal][[/teal][i][green]'disc'[/green][/i][teal]+[/teal]prcLevel[teal]];[/teal]
[teal]}[/teal]


Feherke.
feherke.ga
 
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
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
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]);

[b]or[/b]

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

[b]or[/b] 

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
 
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
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
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
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top