Evik James
Evik James

Reputation: 10473

How to test whether a character is numeric, upper case, or lower case?

I am trying to get this bit of JavaScript to run. I keep getting an error that says:

ThisChar.toUpperCase() is not a function 

I have tried using jsfiddle.net but can't seem to get around this error. What going on with this?

// CREATE ARRAY OF CHARACTERS
CharacterArray = ["1", "2", "3", "a", "b", "c", "D", "E", "F", "/", "+", ")"];

// TEST CHARACTERS 
function testCharacters() {
   // GET ARRAY LEN
   var CharacterArrayLen = CharacterArray.length;
   // LOOP THROUGH ARRAY
   for (i = 0; i < CharacterArrayLen; i++) {
       // PARSE SINGLE CHARACTER
       var ThisChar = CharacterArray.slice(i, i + 1);
       if (!isNaN(ThisChar * 1)) {
           alert(ThisChar + ' is numeric!');
       } else {
           if (ThisChar == ThisChar.toUpperCase()) {
               alert(ThisChar + ' is upper case true');
           } else if (ThisChar == ThisChar.toLowerCase()) {
               alert(ThisChar + " is lower case");
           } else {
               alert(ThisChar + " is unknown");
           }
       }
   }
}
testCharacters();​

Upvotes: 1

Views: 149

Answers (1)

Felix Kling
Felix Kling

Reputation: 816302

.slice always returns an array. Just access the element:

var ThisChar = CharacterArray[i];

You don't have to multiply the value by *1, the value passed to isNaN will be implicitly converted to a number:

!isNaN(ThisChar)

Upvotes: 6

Related Questions