Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #27144 > unrolled thread

help with script

Started byPeCoNe <maljers@telfort.nl>
First post2015-03-30 16:08 +0200
Last post2015-03-31 11:16 +0200
Articles 4 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  help with script PeCoNe <maljers@telfort.nl> - 2015-03-30 16:08 +0200
    Re: help with script JJ <jj4public@vfemail.net> - 2015-03-30 21:44 +0700
    Re: help with script Scott Sauyet <scott.sauyet@gmail.com> - 2015-03-30 19:09 -0700
      Re: help with script "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-03-31 11:16 +0200

#27144 — help with script

FromPeCoNe <maljers@telfort.nl>
Date2015-03-30 16:08 +0200
Subjecthelp with script
Message-ID<551958fb$0$46998$b1db1813$1367588f@news.astraweb.com>
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

[toc] | [next] | [standalone]


#27145

FromJJ <jj4public@vfemail.net>
Date2015-03-30 21:44 +0700
Message-ID<5w0mwhvcstc8$.1qhsfwffxhpb$.dlg@40tude.net>
In reply to#27144
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;
      }
    }
  }

[toc] | [prev] | [next] | [standalone]


#27146

FromScott Sauyet <scott.sauyet@gmail.com>
Date2015-03-30 19:09 -0700
Message-ID<7e5666d0-f59e-4fc1-87ca-d36ea451a936@googlegroups.com>
In reply to#27144
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

[toc] | [prev] | [next] | [standalone]


#27147

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-03-31 11:16 +0200
Message-ID<XnsA46E72BB7CCA3eejj99@194.109.6.166>
In reply to#27146
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)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web