今天我发现了一个条件很差的巨魔。但基本的原则是:“如果我通过了考试,我就必须参加一个聚会。但如果我考试不及格,我就必须专心学习。”
这是一个简单的if,else条件。但是我遇到了太多的问题。我不得不提一下,我正在学习JS。所以简单的问题对我来说可能很难。
我为“考试通过或失败”设置了if、else if、else条件。但当我输入一个“通过条件”以外的值时,我被震惊了。
注意:我将“通过”标记为“好”,将“失败”标记为“坏”。
让我来解释一下我所面对的:
我亲手写了一个剧本。但我遇到了太多未回答的问题。
如果我在这个模式“
最后,我用自己的方式写了一个脚本。可能有任何其他方法来创建此项目,或者可以以其他方式简化脚本。如果你能给我这个建议,我将不胜感激。另外,请建议我如何克服我所面临的问题。没有我的方法,任何解决这些著名问题的替代方法。
关于Md Rizaur
<!--The Script Is Made By Md Rizaur Rahman from https://stackoverflow.com/users/9192572/md-rizaur-rahman-->
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Simple Logic By Md Rizaur Rahman</title>
</head>
<body>
<script>
function logic() {
var userInput = document.getElementById("logic").value;
var input = userInput.toLowerCase();
var onlyStr = /^[a-zA-z]+$/;
var onlyNum =/^[0-9]+$/;
var output;
if(input === "good"){
output = "Lets Party"
}
else if(input === "bad"){
output = "Please Study Attentively"
}
else if ((input.match(onlyStr)) && (input !== "good")){
output = "You Enter String Without \"Good\" Word. Please Enter Good or Bad";
}
else if(input.match(onlyNum)){
output = "You Enter A Number. Please Enter Good or Bad";
}
else if (input.trim() == ""){
output = "Please Enter Any Value or Please Enter Good or Bad";
}
else{
output = "Your Input Type Is Marked As Undefined. Please Enter Any Value or Please Enter Good or Bad";
}
alert (output);
}
</script>
<input type="text" id="logic">
<button onclick="logic()" type="submit" id="input">Submit</button>
<p id="html_output"></p>
</body>
</html>
发布于 2018-08-04 04:18:45
它被标记为未定义,因为它不满足上述任何条件(它与正则表达式不匹配,并且不是“好”或“坏”)。将正则表达式更改为/^[a-zA-z0-9]+$/ (接受0-9之间的数字),并使用!isNaN(input)在该else if前面放入对数值的检查。在检查输入是否等于"good“或"bad”之前,您还应该将输入更改为小写。要使isNaN检查正常工作,您还需要检查input是否全部为空格(if(!input.trim().length)),因为空格也被视为数字(不是NaN)。
<!--The Script Is Made By Md Rizaur Rahman from https://stackoverflow.com/users/9192572/md-rizaur-rahman-->
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Simple Logic By Md Rizaur Rahman</title>
</head>
<body>
<script>
function logic() {
var userInput = document.getElementById("logic").value;
var input = userInput.toLowerCase();
var onlyStr = /^[a-zA-z0-9]+$/;
var onlyNum =/^[0-9]+$/;
var output;
if(input.toLowerCase() === "good"){
output = "Lets Party"
}
else if(input.toLowerCase() === "bad"){
output = "Please Study Attentively"
}
else if(!input.trim().length){
output = "Input can not be empty!";
}
else if(!isNaN(input)){
output = "You Enter A Number. Please Enter Good or Bad";
}
else if ((input.match(onlyStr)) && (input !== "good")){
output = "You Enter String Without \"Good\" Word. Please Enter Good or Bad";
}
else if (input.trim() == ""){
output = "Please Enter Any Value or Please Enter Good or Bad";
}
else{
output = "Your Input Type Is Marked As Undefined. Please Enter Any Value or Please Enter Good or Bad";
}
alert (output);
}
</script>
<input type="text" id="logic">
<button onclick="logic()" type="submit" id="input">Submit</button>
<p id="html_output"></p>
</body>
</html>
https://stackoverflow.com/questions/51679470
复制相似问题