【问题标题】:Someone could explain what is happening with the code?有人可以解释代码发生了什么吗?
【发布时间】:2016-06-19 10:03:34
【问题描述】:

我想猜猜用户输入的是什么类型的字母。

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");
}

【问题讨论】:

    标签: javascript function switch-statement


    【解决方案1】:

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

    【讨论】:

    • 我的错误太愚蠢了...我以为我的变量或参数有问题。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 2015-02-04
    • 1970-01-01
    • 2015-10-16
    • 2020-12-24
    相关资源
    最近更新 更多