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

1. Registered User
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 10:00 AM. Reason: Typo in my title... doh

2. And the reason why you just do not use if..else if...else?

Eric

3. Registered User
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. 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>
<title>Switch Test</title>
<script type="text/javascript">

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>
<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 02:50 PM. Reason: Added test program

5. Registered User
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. ## 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. Registered User
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. ## Something else to consider ...

Originally Posted by peachskittle

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. Registered User
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. ## Consider this ...

Originally Posted by peachskittle
:-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>
<title>Switch Test</title>
<script type="text/javascript">

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>
<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>```

##### 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
•

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.16866 seconds
• Memory Usage 2,961KB
• Queries Executed 15 (?)
Template Usage (34):
• (2)bbcode_code
• (2)bbcode_quote
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (10)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (10)postbit
• (10)postbit_onlinestatus
• (10)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (70):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete