为什么没有任何内容记录到控制台?{JavaScript}.
Why isn't anything logging to the console? {JavaScript}
这是一个判断Minecraft服务器论坛版主应用程序的程序。我想知道为什么除了默认值之外什么都没有输出。请帮帮我。
代码就在这里:
var moderatorApplicant=new Object();
moderatorApplicant.active=true; //boolean;
moderatorApplicant.age=15; //number;
moderatorApplicant.applicationLength="long"; //string, "short" or "long";
console.log("Moderator Application Judge Result:");
switch(moderatorApplicant)
{
case (moderatorApplicant.active===true&&moderatorApplicant.age>=14&&moderatorApplicant.applicationLength=="long"):
console.log("You are fit for a mod! +1");
break;
case (moderatorApplicant.active===true&&moderatorApplicant.age<14&&moderatorApplicant.applicationLength=="long"):
console.log("You're active! You're app is long! You aren't 14 or over though, so +0.");
break;
case (moderatorApplicant.active===true&&moderatorApplicant.age>=14&&moderatorApplicant.applicationLength=="short"):
console.log("App is short, but you're active, and you meet age requirements. +0");
break;
case (moderatorApplicant.active===true&&moderatorApplicant.age<14&&moderatorApplicant.applicationLength=="short"):
console.log("You're active, your app is short, and you are younger than 14. -1");
break;
case (moderatorApplicant.active===false&&moderatorApplicant.age>=14&&moderatorApplicant.applicationLength=="long"):
console.log("You are not active, but you are over 14 and have a long app. Try again when you are more active +0");
break;
case (moderatorApplicant.active===false&&moderatorApplicant.age<14&&moderatorApplicant.applicationLength=="long"):
console.log("You're young, you aren't active, but your app is long. -1");
break;
case (moderatorApplicant.active===false&&moderatorApplicant.age>=14&&moderatorApplicant.applicationLength=="short"):
console.log("You are not active. You made a short app, but you are over 14. -1");
break;
case (moderatorApplicant.active===false&&moderatorApplicant.age<14&&moderatorApplicant.applicationLength=="short"):
console.log("This is the definition of a bad application. Not active, younger than 14, and short app. -1.");
break;
default:
console.log("Check again. Inappropriate values.");
break;
}
在 JavaScript 中,switch 语句需要一个常量值(文字数字或字符串)。您应该使用if/else
语句才能对每个事例使用表达式。
正如Maurício Linhares已经回答的那样,不应该以这种方式使用switch语句。
下面是一个重构实现的示例,使用 if 语句:
var moderatorApplicant= {
active : true, //boolean;
age : 15, //number;
applicationLength : "long" //string, "short" or "long";
}
console.log("Moderator Application Judge Result:");
var message;
if (moderatorApplicant.active) {
if (moderatorApplicant.age >=14) {
if (moderatorApplicant.applicationLength=="long") {
message = "You are fit for a mod! +1";
} else {
message = "App is short, but you're active, and you meet age requirements. +0";
}
} else {
if (moderatorApplicant.applicationLength=="long") {
message = "You're active! You're app is long! You aren't 14 or over though, so +0.";
} else {
message = "You're active, your app is short, and you are younger than 14. -1";
}
}
} else {
if (moderatorApplicant.age >=14) {
if (moderatorApplicant.applicationLength=="long") {
message = "You are not active, but you are over 14 and have a long app. Try again when you are more active +0";
} else {
message = "You are not active. You made a short app, but you are over 14. -1";
}
} else {
if (moderatorApplicant.applicationLength=="long") {
message = "You're young, you aren't active, but your app is long. -1";
} else {
message = "This is the definition of a bad application. Not active, younger than 14, and short app. -1.";
}
}
}
if (!message) message = "Check again. Inappropriate values.";
console.log(message);
下面是另一个示例,改用标志和带有字符串的对象。
var moderatorApplicant= {
active : true, //boolean;
age : 15, //number;
applicationLength : "long" //string, "short" or "long";
}
console.log("Moderator Application Judge Result:");
var messages = {
'ayl' : "This is the definition of a bad application. Not active, younger than 14, and short app. -1.",
'ayL' : "You're young, you aren't active, but your app is long. -1",
'aYl' : "You are not active. You made a short app, but you are over 14. -1",
'aYL' : "You are not active, but you are over 14 and have a long app. Try again when you are more active +0",
'Ayl' : "You're active, your app is short, and you are younger than 14. -1",
'AyL' : "You're active! You're app is long! You aren't 14 or over though, so +0.",
'AYl' : "App is short, but you're active, and you meet age requirements. +0",
'AYL' : "You are fit for a mod! +1"
};
var flags =
(moderatorApplicant.active ? 'A' : 'a') +
(moderatorApplicant.age >= 14 ? 'Y' : 'y') +
(moderatorApplicant.applicationLength=='long' ? 'L' : 'l');
var message = messages[flags];
if (!message) message = "Check again. Inappropriate values.";
console.log(message);
相关文章:
- 为什么我能够从javascript控制台发送跨域ajax请求
- 将数组的拼接分配给自身时,Javascript控制台打印不起作用
- 如何使Visual Studio自动清除Javascript控制台
- 只有当Javascript控制台在chrome上打开时,Javascript才能工作
- 打印javascript控制台的堆栈跟踪
- 如何在调试模式停止后查看Visual Studio 2015 Javascript控制台
- 如何:编写漂亮的HTML代码和Javascript控制台
- 我有可能防止黑客使用网络控制台用JavaScript重复调用upvote(AJAX)函数吗
- 执行JavaScript代码,使用selenium webdriver或WatiN从控制台获取日志
- 调用Javascript时出现控制台错误
- system.err风格的控制台以javascript打印
- 在类似chrome的浏览器中停止控制台javascript命令
- 为什么没有任何内容记录到控制台?{JavaScript}.
- 如何在页面加载时执行控制台 Javascript
- 所有错误都显示在 Chrome 控制台 JavaScript 错误中
- 如何将 neo4j 查询打印到控制台?(Javascript)
- 慢下来,控制台.(javascript/jquery)
- 控制台JavaScript的浏览器兼容性
- 滑块不工作,没有错误在控制台- Javascript香草
- 如何将消息记录到web开发人员控制台-JavaScript