I'm learning JavaScript. Very new and know basic. I'm playing with various options of JavaScript.
I'm comparing a-z (lower case) and A-Z (upper case) from user input. and giving an answer base on the input.
Normally i can do this with this long code:
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;
}
With switch I want to achieve same output but with less code! Something like this:
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;
}
Any Help?
Please I want to do this with only switch function. I know i can do this with if/else function but i want to do this with switch. If its not possible with switch let me know :-)
答案 0 :(得分:3)
You can try it with something like this.
<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>
You can see more info here: https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
答案 1 :(得分:3)
The switch
statement compares the input expression with each case statement using strict comparison.
So use true
inside the switch clause and specify expressions that evaluate to true
in case clause:
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;
}
I personally do not recommend this. This is only for learning.
答案 2 :(得分:2)
You could do a simple if/else
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"););
答案 3 :(得分:1)
Check if the character is in in between other characters:
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!");
答案 4 :(得分:1)
The parameter to a case
statement is something to perform an equality comparison with, you don't put a comparison there. If you want to do a comparison, use if
, not switch
. Also, you should be combining the comparisons with &&
, not ||
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');
}
答案 5 :(得分:1)
Try also:
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")