比较a-z还是a-z,并使用switch JavaScript函数给出输出
Compare if a-z or A-Z and give output with switch JavaScript function
我正在学习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")
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 如何在JavaScript中将字符串转换为函数引用
- 用嵌套函数和默认函数定义函数
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- JQuery合并了keyup和focusout两个函数
- ES6构造函数返回基类的实例
- 监视函数从服务返回不起作用,但作用域函数起作用
- 我可以在json对象中添加一个函数吗
- AngularJS:我可以跳过函数参数回调吗
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- jquery点击函数select&取消选择
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- Node.js v6.2.0类扩展不是函数错误
- 比较从函数和生成的日期对象
- jQuery中是否内置了任何字符串格式化函数
- 当js函数's已执行