我正在研究 switch 语句,我一直在努力使这段代码在这里工作,但它似乎没有输出正确的 console.log case 字符串。
var user = prompt("What is your name?").toLowerCase();
switch(user){
case "luka":
console.log("What a beautiful name, my name is exactly the same :) Hi Luka! :D");
break;
case user.length > 10:
console.log("That's a long name!");
break;
case user.length < 4:
console.log("Not to be rude or impolite to you in any way, but your name is kinda short :( Not that it isn't cool or something :D");
break;
}
我试过像这样 (user).length < 4 那样在用户周围加上括号,但这不管用,我的其他一些尝试也没有用。有人知道如何正确实现吗?
最佳答案
不应在 switch 语句中使用条件。
使用if/else if
var user = prompt("What is your name?").toLowerCase();
if (user==="luka") {
console.log("What a beautiful name, my name is exactly the same :) Hi Luka! :D");
} else if (user.length > 10) {
console.log("That's a long name!");
} else if (user.length < 4) {
console.log("Not to be rude or impolite to you in any way, but your name is kinda short :( Not that it isn't cool or something :D");
} else {
console.log("in else");
}
关于javascript - case user.length < 4;控制台日志 ("nametoo short :(");休息;为什么这不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15274407/