javascript - case user.length < 4;控制台日志 ("nametoo short :(");休息;为什么这不起作用?

标签 javascript switch-statement string-length

我正在研究 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/

相关文章:

java - java POJO中使用注释限制输入长度

javascript - 检查是否定义了对象名称为字符串的对象

javascript - 为 XMLHttpRequest 设置 referer?

javascript - 单击时显示 DataTables 搜索栏

javascript - jQuery 文件不工作

javascript - 为什么 switch 和 if 语句在 Javascript 中对待 boolean 不同,困惑

java - 使用switch语句返回不同的页面实例

Swift 开关盒 "not"

c - 使用旧消息测试输入消息的长度

python - 使用 'max' 和 'len' 正确格式化列表的第 10 项