Javascript Switch Statement

Can anybody help me?

This does not work:

switch (parseInt(charCode)) { case (charCode >= 65 && charCode <=90): //UPPERCASE alert("UP"); break; case (charCode >= 97 && charCode <=122): //LOWERCASE alert("LO"); break; case (charCode >= 48 && charCode <=57): //NNUMBERS alert("NUM"); break } 

thanks

0
source share
5 answers

[ change ]. Add this disclaimer: this switch use is considered EVIL or ABUSIVE. Some Javascript students also see the use of tees as their primary SIN. Therefore, be careful with their use, because hell can wait for you, so the gospel is coming.

You can use switch like this 1 :

 switch (true) { case (parseInt(charCode) >= 65 && parseInt(charCode) <=90): //UPPERCASE alert("UP"); break; case (parseInt(charCode) >= 97 && parseInt(charCode) <=122): //LOWERCASE alert("LO"); break; case (parseInt(charCode) >= 48 && parseInt(charCode) <=57): //NNUMBERS alert("NUM"); break; default: break } 

it is rather bulky. You do not need to use parseInt if you got a charCode coming from event.keyCode . If you need to use parseInt , be sure to provide radix or, better yet, use Number to convert to a number.

In any case, using ternary is an alternative for this:

 alert( charCode >= 65 && charCode <= 90 ? 'UP' : charCode >= 97 && charCode <= 122 ? 'LO' : charCode >= 48 && charCode <= 57 ? 'NUM' : 'OTHER' ); 

[ change ]. See if the following alternative can satisfy Javascript Coders Church ...

Another alternative is to use RegExp with a character derived from keyCode :

 var chr = String.fromCharCode(charCode), alertval = /[az]/.test(chr) ? 'LO' : /[AZ]/.test(chr) ? 'UP' : /[0-9]/.test(chr) ? 'NUM' : 'OTHER'; alert(alertval); 

As a final option (man, javascript is so universal!) I present:

 var check = String.fromCharCode(charCode) .replace(/([az])|([AZ])|([0-9])|(.+)/, function(a,b,c,d) { return b ? 'LO' : c ? 'UP' : d ? 'NUM' : 'OTH'; }); 

1 some notes about this "trick"

+1
source

Cases should be single values, not expressions. If you need to use expressions, use the if else if series:

 var numCharCode = parseInt(charCode, 10); // Always best to include the radix argument if (numCharCode >= 65 && numCharCode <=90) { //UPPERCASE alert("UP"); } else if (numCharCode >= 97 && numCharCode <=122) { //LOWERCASE alert("LO"); } else if (numCharCode >= 48 && numCharCode <=57) { //NNUMBERS alert("NUM"); } 

(Always assuming that charCode indeed a string in the first place. The name assumes that it is already a number.)


Off topic . Your logic is focused on English (and it fails even in English, on words that we copied from other languages, for example, โ€œnaivelyโ€). If this is good, great, but if not, you need to take a closer look at how you reliably define a character's case in a neutral language. More in this answer here at StackOverflow.

+5
source

It should not be assumed that the assumption that Javascript is using an ASCII character table (does it use UTF8?), So instead of comparing numeric values, just use character (string) value comparisons. You should not use the switch statement in this way: as in your example, it does a comparison (value and type, "===") between parseInt (charCode) (which must be an integer) and the values โ€‹โ€‹of boolean expressions, so returned by your statements about affairs. Use the IF chain as follows (suppose charCode is still a string value):

 if (charCode >= 'A' && charCode <='Z') { //UPPERCASE alert("UP"); } else if (charCode >= 'a' && charCode <='z') { //LOWERCASE alert("LO"); break; } else if (charCode >= '0' && charCode <='9'): //NNUMBERS alert("NUM"); } 

For an ASCII numeric value for char, use the "asc" function in Javascript, and therefore, if you are making a charcode from a key event, you can use the inverse, chr (keyCode) function to get the string (character value) of your code already entered. Hope this was helpful :)

+1
source

don't use switch here - this is not the correct syntax, but charCode is int, so there is no need for parseInt it

  if (charCode >= 65 && charCode <=90) //UPPERCASE alert("UP"); else if (charCode >= 97 && charCode <=122) //LOWERCASE alert("LO"); else if (charCode >= 48 && charCode <=57) // NUMBERS alert("NUM"); 

UPDATE: How about fromCharCode

 var char = String.fromCharCode(charCode) if (/[az]/.test(char)) alert("Lower"); else if (/[AZ]/.test(char)) alert("Upper"); else if (/[0-9]/.test(char)) alert("Numbers"); 

http://jsfiddle.net/mplungjan/v65Xc/

I'm sure someone can put this in an object or use a prototype :)

0
source

Please visit this website for features and explanations related to your problem. Sorry, none of them take into account the problem associated with non-ascii characters such as "รฏ" or "รถ", http://javascript.about.com/library/blvalid02.htm

0
source

All Articles