[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

help with script

PeCoNe

3/30/2015 2:09:00 PM

Hallo all,

I am new to js and have the following question:
var Ticker = oStock.Ticker;
if (Ticker != "GD") {
if (Ticker != "OIL") {
if (Ticker != "EURCHF") {
if (Ticker != "EURUSD") {
if (Ticker != 'SPG') {
for (j=oStock.Quotations.Count-1;j>=0;j--) {
tmpDateNum = oStock.Quotations( j ).Date ;
if (tmpDateNum >= DayDeleteFromNum) {
oStock.Quotations.Remove(j);
}
else {
break;
}
}
}
}
}
}
}

Now i must add a few more symbols and i think there is better way then
all that nested if's.
How can i do it in a better way?

Thanks
Peter Maljers
3 Answers

JJ

3/30/2015 2:45:00 PM

0

On Mon, 30 Mar 2015 16:08:59 +0200, PeCoNe wrote:

> Hallo all,
>
> I am new to js and have the following question:
> var Ticker = oStock.Ticker;
> if (Ticker != "GD") {
> if (Ticker != "OIL") {
> if (Ticker != "EURCHF") {
> if (Ticker != "EURUSD") {
> if (Ticker != 'SPG') {
> for (j=oStock.Quotations.Count-1;j>=0;j--) {
> tmpDateNum = oStock.Quotations( j ).Date ;
> if (tmpDateNum >= DayDeleteFromNum) {
> oStock.Quotations.Remove(j);
> }
> else {
> break;
> }
> }
> }
> }
> }
> }
> }
>
> Now i must add a few more symbols and i think there is better way then
> all that nested if's.
> How can i do it in a better way?
>
> Thanks
> Peter Maljers

One method is to use an array. e.g.

var arr = ["GD", "OIL", "EURCHF", "EURUSD", 'SPG']; //as exclusion list
if (arr.indexOf(Ticker) < 0) {
for (j=oStock.Quotations.Count-1;j>=0;j--) {
tmpDateNum = oStock.Quotations( j ).Date ;
if (tmpDateNum >= DayDeleteFromNum) {
oStock.Quotations.Remove(j);
}
else {
break;
}
}
}

Scott Sauyet

3/31/2015 2:09:00 AM

0

PeCoNe wrote:
> [ ... ]
> if (Ticker != "OIL") {
> if (Ticker != "EURCHF") {
> if (Ticker != "EURUSD") {
> if (Ticker != 'SPG') {
> [ ... ]
>
> Now i must add a few more symbols and i think there is better way then
> all that nested if's.

There certainly are, many ways. JJ's suggestion is probably a better
long-term suggestion, as it's easier to extend, but the first thing that
should probably come to mind is to combine your conditions with and, like
this:

> if (Ticker != "OIL" && Ticker != "EURCHF" &&
> Ticker != "EURUSD" && Ticker != 'SPG') {

Best of luck,

-- Scott

Evertjan.

3/31/2015 9:17:00 AM

0

Scott Sauyet <scott.sauyet@gmail.com> wrote on 31 mrt 2015 in
comp.lang.javascript:

> PeCoNe wrote:
>> [ ... ]
>> if (Ticker != "OIL") {
>> if (Ticker != "EURCHF") {
>> if (Ticker != "EURUSD") {
>> if (Ticker != 'SPG') {
>> [ ... ]
>>
>> Now i must add a few more symbols and i think there is better way then
>> all that nested if's.
>
> There certainly are, many ways. JJ's suggestion is probably a better
> long-term suggestion, as it's easier to extend, but the first thing that
> should probably come to mind is to combine your conditions with and, like
> this:
>
>> if (Ticker != "OIL" && Ticker != "EURCHF" &&
>> Ticker != "EURUSD" && Ticker != 'SPG') {

** or use string indexOf():

if (',OIL,EURCHF,EURUSD,SPG,'.indexOf(','+Ticker+',')=-1) {

** or use Regex:

if
(!',OIL,EURCHF,EURUSD,SPG,'.test(new RegExp(','+Ticker+','))){

** or an easily explandable switch:

switch(Ticker) {
case 'OIL': break;
case 'EURCHF': break;
....
default:
{do what you wat to do};
};

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)