我的课程作业是创建一个网页,提示用户他们的年龄,然后决定该用户是否年龄太大/年轻,无法使用AND和OR运算符在青少年运动队中进行游戏。
需要2个功能,每个功能都由自己的按钮调用。一个调用使用AND运算符确定合格性的函数,另一个调用使用OR运算符的函数。
在其他作业中,我没有问题提示用户输入和使用该输入,但每次我尝试在我的函数中使用if语句时,网页就好像没有发生任何事情,当我点击按钮来调用这些功能。
以下代码粘贴:
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css">
<title>Aaron Stockdale's Age Range Page</title>
<script type="text/javascript">
function andFunction()
{
var userAge = prompt("What's your age?", " ");
if(userAge >= 16 && userAge <= 18)
document.getElementById('age').innerHTML = "Age entered " + userAge + "is in range. Can play in youth team.";}
else
document.getElementById('age').innerHTML = "Age entered " + userAge + "is not in range. Can play not in youth team.";}
}
function orFunction()
{
var userAge = prompt("What's your age?", " ");
if(userAge > 15 || userAge < 19)
document.getElementById('age').innerHTML = "Age entered " + userAge + "is in range. Can play in youth team.";}
else
document.getElementById('age').innerHTML = "Age entered " + userAge + "is not in range. Can play not in youth team.";}
}
</script>
</head>
<body>
<header>
<h1>Aaron Stockdale's Age Range Check</h1>
</header>
<section>
<br><p>This page will prompt Aaron for his age to check if he can play in the youth team.</p>
<h2>Click on the button below to enter Aaron's age</h2>
<p><input type="button" id="button1" style="border:2px solid #6A959D" value="Enter age and compare with AND operator"
onclick="andFunction()"/>
<p><input type="button" id="button1" style="border:2px solid #6A959D" value="Enter age and compare with OR operator"
onclick="orFunction()"/>
</section>
</body>
</html>
答案 0 :(得分:2)
你在if和else语句的末尾添加}但没有{打开它们
更改为
if(userAge >= 16 && userAge <= 18) {
document.getElementById('age').innerHTML = "Age entered " + userAge + "is in range. Can play in youth team.";}
else {
document.getElementById('age').innerHTML = "Age entered " + userAge + "is not in range. Can play not in youth team.";}
答案 1 :(得分:1)
除了{没有{,
你有if(userAge > 15 || userAge < 19)
如果有人年龄超过15岁,或者年龄小于19岁,他们可以参加比赛。 所以一个4岁的孩子可以,因为他低于19岁,而一个30岁的孩子也可以,因为他超过15岁......