www.webdeveloper.com
Results 1 to 10 of 10

Thread: Switch of IF? Quick as you can pleeeeeaseee!

  1. #1
    Join Date
    Jun 2009
    Posts
    112

    Switch of IF? Quick as you can pleeeeeaseee!

    Sorry for the mildly obnoxious title.... but here's my situation...

    I need to apply a set of rules to a range of numbers.

    I ask the user for the lowest and the highest number from a range of 0 to 12.

    After I get that range, I need to apply some rules.

    If the range of numbers includes numbers 5 or less, Do A

    If the range of numbers includes 7, Do B

    If the range of numbers includes 10 and above, DoC

    The entire thing has changed scope on me overnight, and originally it was based on key numbers, which I deftly *cough* conquered with a switch.

    But now the switch isn't going to work, at least not without changing it to something like

    switch(true)
    case (comparison):
    DoA;
    break;
    case (comparison):
    DoB;
    break;
    case (comparison):
    DoC;
    break;
    etc...

    But I'm no good with best practices with js. From what I read, this would WORK, but I don't know how people feel about code like that. It doesn't seem natural to me, anyway, but if its okay to use as that, well I'll deal with my own feelings.

    If the switch needs to die, then I'm guessing I need an IF (not if else, as more than one of those rules can apply to any range). If I use an IF, I still need to be comparing the entire range the user gives me, and I hope someone can help me figure the best way to "capture" that range to use for the comparison?

    Thanks thanks thanks...
    Last edited by peachskittle; 09-15-2009 at 11:00 AM. Reason: Typo in my title... doh

  2. #2
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    And the reason why you just do not use if..else if...else?

    Eric

  3. #3
    Join Date
    Jun 2009
    Posts
    112
    Oh wait, I would still use the same for, just take the switch out and replace with if.

    I wasn't going to use if else if else because if a user gives me the full range for example, all three rules apply. I could just use a set of if statement,

    But I really an curious to get some thoughts on that switch, and if its sort of common to do, or a no-no

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,413

    Question

    Your logic setup eludes me.

    What do you mean by a high-low range? Does the user enter two numbers and this is the range?
    If the user enters '0' and '12' does that mean the entire range and if so, do you do DoA, DoB and DoC?

    Or do you mean, the user enters two numbers (hi/lo).
    If one of the numbers is 5 or less, then DoA?
    If one of the numbers is 7, then DoB?
    If one of the numbers is 10 or greater, then DoC?
    Do you want to do both DoA and DoC if the user enters 5,10?
    What if the user enters 6, 8 or 9. Then what happens?

    Or does the user only enter one number and depending on the logic only does either DoA, DoB or DoC ONCE?

    I'm still confused so this is just a guess of your requirements.
    Code:
    <html>
    <head>
    <title>Switch Test</title>
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/showthread.php?t=216738
    
    function DoIt() {
      var action = document.getElementById('range').value;
      var act = 0;
      if (action <= 5) { act = 1; }
      if ((action > 5) && (action < 10)) { act = 2; }
      if (action >= 10) { act = 3; }
      switch (act) {
        case 1 : alert('DoA'); break;
        case 2 : alert('DoB'); break;
        case 3 : alert('DoC'); break;
        default : alert('Do Nothing'); break;  // should never get here
      }
    }
    </script>
    </head>
    <body>
    Enter value from 0-12: 
    <input type="text" id="range" value="">
    <button id="BtnDoIt" onclick="DoIt()">Do Action</button>
    </body>
    </html>
    Good Luck!



    Sorry for so many questions, but you won't get an accurate answer unless you define all actions.

    Remember that a computer chip is usually made of silicon.
    This means that a computer is dumb as dirt!
    Last edited by JMRKER; 09-15-2009 at 03:50 PM. Reason: Added test program

  5. #5
    Join Date
    Jun 2009
    Posts
    112
    Sorry for the confusing post!

    The user enters a low number and a high number, both low and high are required.

    Each time the range of numbers they entered fits one of those rules, the actions for that rule will be done. So a range can have more than 1 thing done to it...

    I took my original switch out and change it to a bunch of IFs. It's ugly, it works, but it's ugly.

    However I'm still curious about the switch thing. Potentially instead of IFs I could have kept my switch statement and have it look for "true" and then have the comparisons as switch cases. and when a number in the range satisfies one of the cases, that case would then be "true"

    Is this like a really bad idea? It looks like it's been done, but feels like a pretty bad idea.

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,413

    Question Still more questions ...

    For sake of my clarity, is this how it is supposed to work (in this example only)

    User chooses 5 and 10 as the low/high values

    Checks 5, does DoA once
    Checks 6, does DoB
    Checks 7, does DoB again
    Checks 8, does DoB again
    Checks 9, does DoB again
    Checks 10, does DoC once.

    Is this the sequence of the actions desired?

    OR

    Would it do each DoA, DoB and DoC function only once for each valid comparison, as in:

    Checks 5, does DoA once
    Checks 6, does nothing
    Checks 7, does DoB once
    Checks 8, does nothing again
    Checks 9, does nothing again
    Checks 10, does DoC once.

  7. #7
    Join Date
    Jun 2009
    Posts
    112


    Well... yes to both. Right now, all it does it toggle show on all those actions. So, VISUALLY, it has no impact either way, since it can be told to show DoA 4 or 5 times, but the visual display will not be affected since its all just repeating itself....

    HOWEVER.... It only needs to do it once, technically... I just didn't do it in a particularly savvy way so I'm using a for to loop through each number in the range then repeating actions over and over...

    *hides face*

  8. #8
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,413

    Lightbulb Something else to consider ...

    Quote Originally Posted by peachskittle View Post


    Well... yes to both. Right now, all it does it toggle show on all those actions. So, VISUALLY, it has no impact either way, since it can be told to show DoA 4 or 5 times, but the visual display will not be affected since its all just repeating itself....

    HOWEVER.... It only needs to do it once, technically... I just didn't do it in a particularly savvy way so I'm using a for to loop through each number in the range then repeating actions over and over...

    *hides face*
    Yes to both ... ... ... ...
    If and ever you decide what you want to do, you might consider this approach.

    Set a flagDoX = false before entering the loop, where X could be variable or an array element.
    If the Action is called upon during the loop, then in the switch checks
    if (flagDoX == false) { DoX(); }
    then set the flagDoX = true so that it cannot execute a second time while in the loop.

    I should then only be able to execute DoA, DoB or DoC only once while in the range of input values.

    Just something to consider when you finalize the design.

    BTW, what kind of program needs this kind of logic checks???

    Good Luck!

  9. #9
    Join Date
    Jun 2009
    Posts
    112
    :-p Well, when I said both, I meant that, both would WORK, and have the same result, though I know my way is probably not the good way.

    I'm not really sure I follow you on your example though...

  10. #10
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,413

    Exclamation Consider this ...

    Quote Originally Posted by peachskittle View Post
    :-p Well, when I said both, I meant that, both would WORK, and have the same result, though I know my way is probably not the good way.

    I'm not really sure I follow you on your example though...
    Anyway that works to your satisfaction is a good way.

    My attempt to solve your problem by calling DoA, DoB or DoC only one time:
    Code:
    <html>
    <head>
    <title>Switch Test</title>
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/showthread.php?t=216738
    
      var flagDoA = false;
      var flagDoB = false;
      var flagDoC = false;
    
    function Actions(act) {
      switch (act) {
        case 1 : if (flagDoA == false) { alert('DoA'); flagDoA = true; } break;
        case 2 : if (flagDoB == false) { alert('DoB'); flagDoB = true; } break;
        case 3 : if (flagDoB == false) { alert('DoC'); flagDoC = true; } break;
        default : alert('Do Nothing'); break;  // should never get here
      }
    }
      
    function DoIt() {
      var LOW = document.getElementById('LOWrange').value;
      var HIGH = document.getElementById('HIrange').value;
      flagDoA = false;
      flagDoB = false;
      flagDoC = false;
      
      var act = 0;
      if (LOW <= 5) { act = 1; }
      if ((LOW > 5) && (action < 10)) { act = 2; }
      if (LOW >= 10) { act = 3; }
      Actions(act);
      
      act = 0;
      if (HIGH <= 5) { act = 1; }
      if ((HIGH > 5) && (action < 10)) { act = 2; }
      if (HIGH >= 10) { act = 3; }
      Actions(act);
    }
    </script>
    </head>
    <body>
    Enter values from 0-12: 
    <input type="text" id="LOWrange" value="">
    <input type="text" id="HIrange" value="">
    <button id="BtnDoIt" onclick="DoIt()">Do Action</button>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles