...

View Full Version : Optimize switch statements



saltoceana
10-12-2011, 08:30 AM
Currently I have the following javascript switch statement ...



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


case "aa": case "Aa": case "aA": case "AA": orderlist[1] += addthis; break;


... for each two-character combination. (aa to zz)

Appreciate any help.

siberia-man
10-12-2011, 10:56 AM
Uuuughhhly code!!!

Let's solve the first issue:


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:

Kor
10-12-2011, 01:05 PM
Currently I have the following javascript switch statement ...
What, in fact, should do that code?

saltoceana
10-13-2011, 04:36 AM
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):



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

saltoceana
10-13-2011, 04:37 AM
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.

jmrker
10-13-2011, 05:19 AM
Have you tried this?


<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

Old Pedant
10-13-2011, 07:20 AM
I am afraid this method [string.charCodeAt] will not work nicely because ASCII of 'a' is 97 and ASCII of 'A' is 65.

Nonsense.



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.

jassi.singh
10-13-2011, 08:52 AM
Infact please post what you want to achieve, we can suggest some better and clean way

Kor
10-13-2011, 09:34 AM
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..

siberia-man
10-13-2011, 10:23 AM
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):



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


Heh... You just don't have experience in preparing that (-: Look this:



// 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:


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:


// 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) ) {

siberia-man
10-13-2011, 10:43 AM
And finally... Remember parseInt function.



// 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:



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

saltoceana
10-20-2011, 02:57 AM
And finally... Remember parseInt function.


Yes, that parseInt will complete my picture.

Thanks everyone for your help :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum