2016-06-19 61 views
-2

我想猜猜用户输入什么类型的字母。有人可以解释代码中发生了什么?

var userLetter = prompt("Enter a letter and I will tell you what type of letter is","Enter here, wish me luck!"); 

function selectUserLetter(letter) { 
    var returnType = "NA"; 

    if (userLetter.charCodeAt(0) >= "A".charCodeAt(0) && userLetter.charCodeAt(0) <= "Z".charcodeAt(0)) { 
     returnType = "U"; 
    } 
    else if (userLetter.charCodeAt(0) >= "a".charCodeAt(0) && userLetter.charCodeAt(0) <= "z".charcodeAt(0)) { 
     returnType = "L"; 
    } 
    else if (userLetter.charCodeAt(0) >= "0".charCodeAt(0) && userLetter.charCodeAt(0) <= "9".charcodeAt(0)) { 
     returnType = "N"; 
    } 

    return returnType; 
} 

switch (selectUserLetter(userLetter)) { 
    case "U": 
     document.write("Your letter is Uppercase"); 
     break; 

    case "L": 
     document.write("Your letter is Lowercase"); 
     break; 

    case "N": 
     document.write("Your letter is a number"); 
     break; 

    default: 
     document.write("You typed anything else"); 
} 

回答

3

在代码中,片段"Z".charcodeAt"z".charcodeAt(0)"9".charcodeAt(0)charcodeAt函数调用。问题在于JavaScript是大小写繁杂的语言。所以,charcodeAt不存在,而不是charCodeAt

+0

这么傻我的错误...我想我有一些问题的变量或参数。非常感谢! –

相关问题