比较a-z还是a-z,并使用switch JavaScript函数给出输出

Compare if a-z or A-Z and give output with switch JavaScript function

本文关键字:a-z 函数 JavaScript 输出 switch 还是 比较      更新时间:2023-09-26

我正在学习JavaScript。非常新,非常基础。我在玩各种各样的JavaScript选项。

我从用户输入中比较a-z(小写)和a-z(大写)。并根据输入给出答案。

通常情况下,我可以用这个长代码做到这一点:

var x = prompt("Enter Your character");
switch (x) {
    case 'a':
    case 'b':
    case 'c':
    case 'd':
    case 'e':
    case 'f':
    case 'g':
    case 'h':
    case 'i':
    case 'j':
    case 'k':
    case 'l':
    case 'm':
    case 'n':
    case 'o':
    case 'p':
    case 'q':
    case 'r':
    case 's':
    case 't':
    case 'u':
    case 'v':
    case 'w':
    case 'x':
    case 'y':
    case 'z':
        document.write("Lower case");
        break;
    case 'A':
    case 'B':
    case 'C':
    case 'D':
    case 'E':
    case 'F':
    case 'G':
    case 'H':
    case 'I':
    case 'J':
    case 'K':
    case 'L':
    case 'M':
    case 'N':
    case 'O':
    case 'P':
    case 'Q':
    case 'R':
    case 'T':
    case 'U':
    case 'V':
    case 'W':
    case 'X':
    case 'Y':
    case 'Z':
        document.write("Upper case");
        break;
    default:
        document.write("It is number");
        break;
}

有了开关,我想用更少的代码实现相同的输出!类似这样的东西:

var x = prompt("Enter Your character");
switch(x) {
    case x >= 'a'|| x <= 'z':
        document.write("Lower case");
        break;
    case x >= 'A' || x <= 'Z':
        document.write("Upper case");
        break;
    default:
        document.write("It is number");
        break;
}

有什么帮助吗?

请允许我只使用开关功能。我知道我可以用if/else函数来做这件事,但我想用switch来做。如果开关不可能,请告诉我:-)

switch语句使用严格的比较将输入表达式与每个case语句进行比较。

因此,在switch子句中使用true,并在case子句中指定求值为true的表达式:

var x = prompt("Enter Your character");
switch (true) {
  case x >= "a" && x <= "z":
    alert("Lower case");
    break;
  case x >= "A" && x <= "Z":
    alert("Upper case");
    break;
  default:
    alert("Something else");
    break;
}

我个人不建议这样做。这只是为了学习

您可以用这样的东西来尝试。

<script>
var x=prompt("Enter Your character");
if (x.test(/[a-z]/)) {
document.write("Lower case");
} else if (x.test(/[A-Z]/)) {
document.write("Upper case");
} else {
document.write("It is number");
}
</script>

您可以在此处查看更多信息:https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test

你可以做一个简单的if/elset

var x=prompt("Enter Your character");
if(!isNaN(x))
    console.log("It is number");
else if(x.toLowerCase()==x)
    console.log("Lower case");
else if(x.toUpperCase()==x)
    console.log("Upper case"););

检查字符是否在其他字符之间:

var x=prompt("Enter Your character");
if (x >= '0' && x <= '9')
    alert("number!");
else if(x >= 'a' && x <= 'z')
    alert("lowercase!");
else if(x >= 'A' && x <= 'Z')
    alert("uppercase!");
else
    alert("not a letter!");

case语句的参数是用来执行相等比较的参数,您不需要在那里进行比较。如果要进行比较,请使用if,而不是switch。此外,您应该将比较与&&相结合,而不是与|| 相结合

if (x >= 'a' && x <= 'z') {
    document.write('Lower case');
} else if (x >= 'A' && x <= 'Z') {
    document.write('Upper case');
} else if (x >= '0' && x <= '9') {
    documennt.write('Number');
} else {
    document.write('Something else');
}

同时尝试:

var x = prompt("Enter Your character");
var find = x.match(/([a-z]*)([A-Z]*)([0-9]*)/);
var type = ["Lower case","Upper case", "It is number"];
document.write(find ? type[find.lastIndexOf(x)-1] : "Unknown char")