Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have at least four options:</p> <h2>1. List each <code>case</code></h2> <p>As <a href="https://stackoverflow.com/a/17145741/157247">shown by LightStyle</a>, you can list each case explicitly:</p> <pre><code>switch(myInterval){ case 0: case 1: case 2: doStuffWithFirstRange(); break; case 3: case 4: case 5: doStuffWithSecondRange(); break; case 6: case 7: doStuffWithThirdRange(); break; default: doStuffWithAllOthers(); } </code></pre> <h2>2. Use <code>if</code> / <code>else if</code> / <code>else</code></h2> <p>If the ranges are large, that gets unwieldy, so you'd want to do ranges. Note that with <code>if...else if...else if</code>, you don't get to the later ones if an earlier one matches, so you only have to specify the upper bound each time. I'll include the lower bound in <code>/*...*/</code> for clarity, but normally you would leave it off to avoid introducing a maintenance issue (if you include both boundaries, it's easy to change one and forget to change the other):</p> <pre><code>if (myInterval &lt; 0) { // I'm guessing this is an error } else if (/* myInterval &gt;= 0 &amp;&amp; */ myInterval &lt;= 2){ doStuffWithFirstRange(); } else if (/* myInterval &gt;= 3 &amp;&amp; */ myInterval &lt;= 5) { doStuffWithSecondRange(); } else if (/* myInterval &gt;= 6 &amp;&amp; */ myInterval &lt;= 7) { doStuffWithThirdRange(); } else { doStuffWithAllOthers(); } </code></pre> <h2>3. Use <code>case</code> with expressions:</h2> <p>JavaScript is unusual in that you can use expressions in the <code>case</code> statement, so we can write the <code>if...else if...else if</code> sequence above as a <code>switch</code> statement:</p> <pre><code>switch (true){ case myInterval &lt; 0: // I'm guessing this is an error break; case /* myInterval &gt;= 0 &amp;&amp; */ myInterval &lt;= 2: doStuffWithFirstRange(); break; case /* myInterval &gt;= 3 &amp;&amp; */ myInterval &lt;= 5: doStuffWithSecondRange(); break; case /* myInterval &gt;= 6 &amp;&amp; */ myInterval &lt;= 7: doStuffWithThirdRange(); break; default: doStuffWithAllOthers(); } </code></pre> <p>I'm not advocating that, but it <em>is</em> an option in JavaScript, and there are times it's useful. The <code>case</code> statements are checked <strong>in order</strong> against the value you give in the <code>switch</code>. <em>(And again, lower bounds could be omitted in many cases because they would have matched earlier.)</em> Even though the <code>case</code>s are processed in source-code order, the <code>default</code> can appear anywhere (not just at the end) and is only processed if either no <code>case</code>s matched or a <code>case</code> matched and fell through to the default (didn't have a <code>break</code>; it's rare you want to do that, but it happens).</p> <h2>4. Use a dispatch map</h2> <p>If your functions all take the same arguments (and that could be no arguments, or just the same ones), another approach is a dispatch map:</p> <p>In some setup code:</p> <pre><code>var dispatcher = { 0: doStuffWithFirstRange, 1: doStuffWithFirstRange, 2: doStuffWithFirstRange, 3: doStuffWithSecondRange, 4: doStuffWithSecondRange, 5: doStuffWithSecondRange, 6: doStuffWithThirdRange, 7: doStuffWithThirdRange }; </code></pre> <p>Then instead of the switch:</p> <pre><code>(dispatcher[myInterval] || doStuffWithAllOthers)(); </code></pre> <p>That works by looking up the function to call on the <code>dispatcher</code> map, defaulting to <code>doStuffWithAllOthers</code> if there's no entry for that specific <code>myInterval</code> value using <a href="http://blog.niftysnippets.org/2008/02/javascripts-curiously-powerful-or.html" rel="noreferrer">the curiously-powerful <code>||</code> operator</a>, and then calling it.</p> <p>You can break that into two lines to make it a bit clearer:</p> <pre><code>var f = dispatcher[myInterval] || doStuffWithAllOthers; f(); </code></pre> <p>I've used an object for maximum flexibility. You <em>could</em> define <code>dispatcher</code> like this with your specific example:</p> <pre><code>var dispatcher = [ /* 0-2 */ doStuffWithFirstRange, doStuffWithFirstRange, doStuffWithFirstRange, /* 3-5 */ doStuffWithSecondRange, doStuffWithSecondRange, doStuffWithSecondRange, /* 6-7 */ doStuffWithThirdRange, doStuffWithThirdRange ]; </code></pre> <p>...but if the values aren't contiguous numbers, it's much clearer to use an object instead.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload