Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    37
    Thanks
    13
    Thanked 1 Time in 1 Post

    Question Optimize switch statements

    Currently I have the following javascript switch statement ...

    Code:
    		switch(firstchar){
    		   case "0": case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9":
    		   orderlist[0] += addthis; break;
    		   case "a": case "A": orderlist[1] += addthis; break;
    		   case "b": case "B": orderlist[2] += addthis; break;
    		   case "c": case "C": orderlist[3] += addthis; break;
    		   case "d": case "D": orderlist[4] += addthis; break;
    		   case "e": case "E": orderlist[5] += addthis; break;
    		   case "f": case "F": orderlist[6] += addthis; break;
    		   case "g": case "G": orderlist[7] += addthis; break;
    		   case "h": case "H": orderlist[8] += addthis; break;
    		   case "i": case "I": orderlist[9] += addthis; break;
    		   case "j": case "J": orderlist[10] += addthis; break;
    		   case "k": case "K": orderlist[11] += addthis; break;
    		   case "l": case "L": orderlist[12] += addthis; break;
    		   case "m": case "M": orderlist[13] += addthis; break;
    		   case "n": case "N": orderlist[14] += addthis; break;
    		   case "o": case "O": orderlist[15] += addthis; break;
    		   case "p": case "P": orderlist[16] += addthis; break;
    		   case "q": case "Q": orderlist[17] += addthis; break;
    		   case "r": case "R": orderlist[18] += addthis; break;
    		   case "s": case "S": orderlist[19] += addthis; break;
    		   case "t": case "T": orderlist[20] += addthis; break;
    		   case "u": case "U": orderlist[21] += addthis; break;
    		   case "v": case "V": orderlist[22] += addthis; break;
    		   case "w": case "W": orderlist[23] += addthis; break;
    		   case "x": case "X": orderlist[24] += addthis; break;
    		   case "y": case "Y": orderlist[25] += addthis; break;
    		   case "z": case "Z": orderlist[26] += addthis; break;
    		   default: alert('Illegal Entry'); break;
    		}
    ... that I wish to optimize so that I do not need to write out so many lines of code.

    Also, I wish to take into the following as consideration ...
    Code:
    		   case "aa": case "Aa": case "aA": case "AA": orderlist[1] += addthis; break;
    ... for each two-character combination. (aa to zz)

    Appreciate any help.
    Last edited by saltoceana; 10-12-2011 at 09:25 AM.

  • #2
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Uuuughhhly code!!!

    Let's solve the first issue:

    Quote Originally Posted by saltoceana
    I wish to optimize so that I do not need to write out so many lines of code
    Use the String#charCodeAt method to gather an ASCII code of a letter.
    Reduce it to the number within the range 0..26, where
    characters from '0' to '9' corresponds 0,
    'A' and 'a' corresponds 1, ...
    'Z' and 'z' corresponds 26.

    I gave the verbal algorythm. Sure you are able to implement it in javascript:

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by saltoceana View Post
    Currently I have the following javascript switch statement ...
    What, in fact, should do that code?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    New Coder
    Join Date
    Oct 2011
    Posts
    37
    Thanks
    13
    Thanked 1 Time in 1 Post
    Quote Originally Posted by siberia-man View Post
    Use the String#charCodeAt method to gather an ASCII code of a letter.
    I am afraid this method will not work nicely because ASCII of 'a' is 97 and ASCII of 'A' is 65.

    I would like something like the following (or better):

    Code:
     case 'a' | 'A': ... break;

  • #5
    New Coder
    Join Date
    Oct 2011
    Posts
    37
    Thanks
    13
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Kor View Post
    What, in fact, should do that code?
    Hi Kor, please see a bit below further, you will notice a line as following (after the coding):

    ... that I wish to optimize so that I do not need to write out so many lines of code.

  • #6
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,146
    Thanks
    39
    Thanked 505 Times in 499 Posts

    Lightbulb Something a little less complicated...

    Have you tried this?
    Code:
    <script type="text/javascript">
      firstchar = firstchar.toUpperCase();
      if ((firstchar >= '0') && (firstchar <= '9')) { orderlist[0] += addthis; }
      else {
        var charOrd = ord(firstchar) - ord('A');
        if ((charOrd >= 0) && (charOrd < 26)) { orderlist[charOrd+1] += addthis; }
        else { alert('Illegal Entry'); }
      }
    </script>
    Note, code is untested because I'm not sure where 'firstchar', 'orderlist' or 'addthis' are defined

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,603
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    Quote Originally Posted by saltoceana View Post
    I am afraid this method [string.charCodeAt] will not work nicely because ASCII of 'a' is 97 and ASCII of 'A' is 65.
    Nonsense.

    Code:
        firstchar = firstchar.toUpperCase();
        if ( firstchar >= "0" && firstchar <= "9") orderlist[0] += addthis;
        else if ( firstchar >= "A" && firstchar <= "Z" ) orderlist[firstchar.charCodeAt(0)-64] += addthis;
        else alert( "Illegal entry" );
    This is essentially identical to jmrker's code, just using charCodeAt instead. You know, the method you said wouldn't work.

    *********
    Also, I wish to take into the following as consideration ...
    Code:
    case "aa": case "Aa": case "aA": case "AA": orderlist[1] += addthis; break;
    Ummm...and what is the outcome if firstchar is, say, "AB" or "9A" or "B7" or...

    Would help if you gave the *actual* problem, not just a fragment.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Regular Coder
    Join Date
    Sep 2011
    Posts
    103
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Infact please post what you want to achieve, we can suggest some better and clean way

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by saltoceana View Post
    Hi Kor, please see a bit below further, you will notice a line as following (after the coding):

    ... that I wish to optimize so that I do not need to write out so many lines of code.
    I asked you what your code should do, not why do you want to modify the code..
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #10
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Quote Originally Posted by saltoceana View Post
    I am afraid this method will not work nicely because ASCII of 'a' is 97 and ASCII of 'A' is 65.

    I would like something like the following (or better):

    Code:
     case 'a' | 'A': ... break;
    Heh... You just don't have experience in preparing that (-: Look this:

    Code:
    // get an ASCII of the first character
    var chr = string.charCodeAt(0);
    
    // consider this as capital Latins
    // A..Z returns 1..26
    // a..z returns 33..58
    // 0..9 returns -16..-7
    var dif = chr - 'A'.charCodeAt(0) + 1;
    
    // reduce 33..58 to 1..26
    var idx = dif % 32;
    
    // make all negative numbers equal to 0
    var k = Math.max(0, idx);
    As summary we can combine these above to the following single string:
    Code:
    var k = Math.max(0, (c.charCodeAt(0) - 'A'.charCodeAt(0) + 1) % 32);
    k == 0 for all digist
    k == 1 for 'A' or 'a'
    ...
    k == 26 for 'Z' or 'z'

    But before performing these mathematical operations you have check that all characters are alphanumeric only -- performing some checks using usual conditions or regular expressions:
    Code:
    // here is something that wipes difference between 'A' and 'a'
    var chr = string.charCodeAt(0).toUpperCase();
    if ( chr >= '0' && chr <= '9' || chr >= 'A' && chr <= 'Z' ) {
    
    
    if ( string.match(/^[0-9A-Z]/i) ) {
    
    if ( /^[0-9A-Z]/i.test(string) ) {

  • #11
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    And finally... Remember parseInt function.

    Code:
    // NOTE: I took the character only, not an ASCII!!!
    var chr = string.charAt(0);
    
    // Consider the 36 numeral system (0..9, A..Z)
    // '0' -> 0
    // '9' -> 9
    // 'A' or 'a' -> 10
    // 'Z' or 'z' -> 36
    var idx = parseInt(chr, 36);
    
    // This line is the same as in the example above
    var k = Math.max(0, idx - 9);
    If chr contains invalid character (not in the ranges '0'..'9', 'A'..'Z', 'a'..'z') than there will be not-a-number that can be checked with function isNaN:

    Code:
    var chr = string.charAt(0);
    var k = Math.max(0, parseInt(chr, 36) - 9);
    
    if ( isNaN(k) ) {
        alert('This is not alphanumeric character');
        return;
    }
    
    // Do something useful

  • Users who have thanked siberia-man for this post:

    saltoceana (10-20-2011)

  • #12
    New Coder
    Join Date
    Oct 2011
    Posts
    37
    Thanks
    13
    Thanked 1 Time in 1 Post
    Quote Originally Posted by siberia-man View Post
    And finally... Remember parseInt function.
    Yes, that parseInt will complete my picture.

    Thanks everyone for your help


  •  

    Posting Permissions

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