0

Can anyone help me?

This doesn't seem to 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

thebounder
  • 3
  • 1
  • 2

5 Answers5

5

Cases must be single values, not expressions. If you need to use expressions, use an 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 charCode really is a string in the first place. The name suggests it's already a number.)


Off-topic: Your logic is English-centric (and fails even for English, on words we've copied from other languages, like "naïve"). If that's okay, great, but if not you'll want to look more into how you reliably determine the case of a character in a language-neutral way. More in this answer here on StackOverflow.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Have remembered how to do it myself: charCode = parseInt(charCode); switch (true) { case (charCode >= 65 && charCode <=90): //UPPERCASE // return true; alert("UP"); break; case (charCode >= 97 && charCode <=122): //UPPERCASE // return true; alert("LO"); break; case (charCode >= 48 && charCode <=57): //NUMBERS // return true; alert("NUM"); break; case (charCode == 8 || charCode == 13): return true; break; default: return false; break; } – thebounder Jun 18 '11 at 09:55
  • Actually it even fails for some "original" English words (definition tricky due to how the language came to be). "encyclopædia" comes to mind. – Lightness Races in Orbit Jun 18 '11 at 14:22
  • @Tomalek: "encyclopædia" is copied from Greek, indirectly via Latin, which is why it has the ash diphthong ("æ") which is fairly common in transliterations of words from the Greek to Latin alphabets. (English is *such* a mutt. :-) ) But again, exactly, this is an English word and has been for nearly 500 years, and for most of that time it was spelt with the diphthong. – T.J. Crowder Jun 18 '11 at 14:36
1

[edit] Let's add this disclaimer: this use of switch is considered EVIL or ABUSIVE. Some Disciples Of The Javascript also consider the use of ternaries as a major SIN. So beware of using them, because hell may await you, so the Gospel goes.

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's pretty bulky. You don't have to use parseInt if you derive charCode came from event.keyCode. If you need to use parseInt, don't forget to provide the radix, or better still, use Number to convert to a number.

Anyway using a ternary is an alternative for this:

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

[edit] Let's see if the following alternative can satisfy the church of Javascript Coders...

Another alternative is using a RegExp with the character derived from the keyCode:

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

As a last alternative (man, javascript is so versatile!) I present:

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

1 some notes on that 'trick'

KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • 5
    What ... a ... *horrible* ...idea. :-) Still, though, it should *work*, cases *are* evaluated in order. – T.J. Crowder Jun 18 '11 at 09:55
  • Thanks, I remembered that's how to do it. It is the exact correct method and far better than a string of ugly if else statements. – thebounder Jun 18 '11 at 09:57
  • 2
    @thebounder: Ugliness, like beauty, is in the eye of the beholder. :-) – T.J. Crowder Jun 18 '11 at 09:58
  • I remember now that I forgot that and I am so happy I did forget it. – mplungjan Jun 18 '11 at 09:59
  • @all: that's why I provided an alternative ;~) – KooiInc Jun 18 '11 at 10:01
  • Oh yeah, let's see if we can abuse other language constructs to make it as obscure as possible ;))) – mplungjan Jun 18 '11 at 10:02
  • Horrible idea, also supposing this could work as expected, it should make use of side effects of operators, making (at least) the source unreadable... :( – Dmitri Sologoubenko Jun 18 '11 at 10:03
  • @D.S. let's say unreadability [also] is in the eye of the beholder. – KooiInc Jun 18 '11 at 10:08
  • @Koolinc, TRUST is in the eye of the beholder, the BEAUTY too. But not unreadability. Why making a simple thing so complex? – Dmitri Sologoubenko Jun 18 '11 at 10:12
  • Phew, amazing how easy it is to 'irritate people and make things generally annoying' (after Monty Python). Is it the weather? Should I change my (gr)avatar? Have I fallen out of grace? Am I using the wrong javascript scriptures (the ones where `ternaries` are ok)? Who knows? I just wanted to provide the OP with an answer and some alternatives. – KooiInc Jun 18 '11 at 10:57
  • My personal irritation came from your String.formCharCode example which is in my opinion a rehash of my simple `if (regexp.test())` suggestion with your horrific ternary. – mplungjan Jun 18 '11 at 12:37
  • Sorry about that @mplungjan, but I think we were editing around the same time on the regexp.test answer. I didn't read your edit before I added my `.test` solution, that much I know. Anyway, I suppose the OP has choice enough now. AFAIAC ... next question please. – KooiInc Jun 18 '11 at 12:58
1

You shouldn't take as true the supposition about Javascript uses ASCII character table (doesn't it use UTF8?), so instead of numerical value comparison, just use character (string) value comparison. You shouldn't use switch statement in such a way: as in your example, it performs a (value and type, "===") comparison between parseInt(charCode) (which should be an integer) and boolean expression values such that returned by your case statements. Use and IF chain, as follows (supposing 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 ASCII numerical value of a char, use Javascript "asc" function, and so if you charcode is coming from a key event, you can use the inverse, the chr(keyCode) function in order to obtain a string (character) value of your already numerical keycode. I hope it was helpful :)

Dmitri Sologoubenko
  • 2,909
  • 1
  • 23
  • 27
  • I think you need to do String.fromCharCode here – mplungjan Jun 18 '11 at 10:16
  • 1
    *"You shouldn't take as true the supposition about Javascript uses ASCII character table (doesn't it use UTF8?)"* It uses UTF-16, actually. But it doesn't matter in this case. ASCII and Unicode share the same first 127 values, by design. The bigger issue is that this is a very naïve definition of "upper case" and "lower case" that fails easily. For instance, looking at that last sentence, is the "ï" in "naïve" lower case? I think it is, I'm pretty sure the French think it is, but this code does not. (And "naïve" is an English word, we just copied it from the French a while back.) – T.J. Crowder Jun 18 '11 at 10:43
  • @TjCrowder: And the non-borrowed "encyclopædia" vs "ENCYCLOPÆDIA". – Lightness Races in Orbit Jun 18 '11 at 14:24
  • how does one type Æ on a keyboard?(I cut and paste it) – Yevgeny Simkin Jun 18 '11 at 16:17
0

don't use switch here - it is not correct syntax and charCode is an int so no need to 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 (/[a-z]/.test(char)) alert("Lower");
else if (/[A-Z]/.test(char)) alert("Upper");
else if (/[0-9]/.test(char)) alert("Numbers");

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

I am sure someone can put this in an object or use prototype :)

mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

Please visit this website for functions and explanation related to your problem. Sorry but none of them takes in consideration the problem related to non-ascii chars like 'ï' or 'ö' http://javascript.about.com/library/blvalid02.htm

Dmitri Sologoubenko
  • 2,909
  • 1
  • 23
  • 27