Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript if/else问题:';是”;被视为';否';因为区分大小写_Javascript_If Statement - Fatal编程技术网

JavaScript if/else问题:';是”;被视为';否';因为区分大小写

JavaScript if/else问题:';是”;被视为';否';因为区分大小写,javascript,if-statement,Javascript,If Statement,我是学习JS的新手,我写了自己版本的一个练习,试图记住我所学的内容。以下是我写的: <!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <script> alert("

我是学习JS的新手,我写了自己版本的一个练习,试图记住我所学的内容。以下是我写的:

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<script>

alert("Good morning!");
var hire = prompt("Are you here because you're interested in hiring me?");

if (hire === "yes") {
    alert("You've just made my day. Carry on.")
}

else {
    alert("Well, I hope you're at least thinking about it.")
}

</script>

</body>
</html>

无标题文件
警惕(“早上好!”);
var hire=prompt(“你来这里是因为你对雇用我感兴趣吗?”);
如果(雇用==“是”){
警惕(“你让我高兴极了,继续吧。”)
}
否则{
警惕(“嗯,我希望你至少正在考虑此事。”)
}

问题是,如果你没有在所有小写字母中键入“是”,它会假定答案属于“else”类别,并吐出错误的答案。我一直在阅读有关将其转换为大写的方法,但我仍然不知道它需要什么额外的代码。谢谢。

您应该使用
.toLowerCase()
,以强制他们以小写形式响应。然后你可以检查它是否总是小写

if (hire.toLowerCase() === 'yes') {
    alert('You\'ve just made my day. Carry on.');
} else {
    alert('Well, I hope you\'re at least thinking about it.');
}
这样就好了

    <!DOCTYPE html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>

    <script>

    alert("Good morning!");
    var hire = prompt("Are you here because you're interested in hiring me?");

    if (hire.toLowerCase() === "yes") {
        alert("You've just made my day. Carry on.")
    }

    else {
        alert("Well, I hope you're at least thinking about it.")
    }

    </script>

    </body>
    </html>

无标题文件
警惕(“早上好!”);
var hire=prompt(“你来这里是因为你对雇用我感兴趣吗?”);
if(hire.toLowerCase()=“是”){
警惕(“你让我高兴极了,继续吧。”)
}
否则{
警惕(“嗯,我希望你至少正在考虑此事。”)
}

@Jason未确认…@adeneo-这只是一个练习,我仍在掌握if/else语句的诀窍,并使用提示。当我回答时,您的答案还没有到达。时间戳请求您的帮助,我非常感谢。