!< in Javascript - 如何仅在用户未满 18 岁时将我的条件设置为 运行?
!< in Javascript - How to set my condition to run only when the user is not under 18?
我想将条件设置为,如果它不小于 18 则可以投票,否则不符合条件。花了一个多小时,但无法弄清楚。我认为问题出在 !操作员。我尝试过的一些组合如下:
如果(符合条件!< 18)
if(!eligible < 18)
if(!eligible >= 18)
这是我正在处理的代码。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test a logical NOT condition</title>
<script type="text/javascript">
var eligible = Number(prompt("How old are you?", 20));
if (eligible! < 18) {
document.write("You are eligible to enrol to vote.");
} else {
document.write("You are too young to vote. Come back when you are 18");
}
</script>
</head>
<body>
</body>
</html>
您放错了 !
。
if(!(eligible < 18))
你写的其实是在检查eligible
不为0 等同于:
if((!eligible) < 18))
您需要将 !
标记放在 之前 您也想检查的条件。
我想将条件设置为,如果它不小于 18 则可以投票,否则不符合条件。花了一个多小时,但无法弄清楚。我认为问题出在 !操作员。我尝试过的一些组合如下:
如果(符合条件!< 18)
if(!eligible < 18)
if(!eligible >= 18)
这是我正在处理的代码。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test a logical NOT condition</title>
<script type="text/javascript">
var eligible = Number(prompt("How old are you?", 20));
if (eligible! < 18) {
document.write("You are eligible to enrol to vote.");
} else {
document.write("You are too young to vote. Come back when you are 18");
}
</script>
</head>
<body>
</body>
</html>
您放错了 !
。
if(!(eligible < 18))
你写的其实是在检查eligible
不为0 等同于:
if((!eligible) < 18))
您需要将 !
标记放在 之前 您也想检查的条件。