我正在尝试运行此JavaScript。我不断收到错误消息:

ThisChar.toUpperCase() is not a function


我尝试使用jsfiddle.net,但似乎无法解决此错误。这是怎么回事?

// 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();​

最佳答案

.slice始终返回一个数组。只需访问元素:

var ThisChar = CharacterArray[i];




您不必将值乘以*1,传递给isNaN的值将隐式转换为数字:

!isNaN(ThisChar)

10-06 11:32