I am new to JS and taking a online class, I cant determine why this function does not return the intended value of 20.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type = "text/javascript">
function calcpts(points)
{
if (window.document.jsquiz.rad1[0].checked == true)
{
var pts = 0;
pts = pts + parseint(20);
alert(pay);
return pay;
}
}
</script>
</head>
<body>
<h1>JavaScript Quiz</h1>
<form name = "jsquiz">
<p>Question #1 You can test a condition with an if..else statement or with an if..elseif..else statement</p>
<input type = "radio" name = "rad1" value="ques">True<br>
<input type = "radio" name = "rad1" value="ques">False<br><br>
<input type="button" name="toClick" value="calculate" onclick="grossPts.value = calcpts(points)" >
</p>
<br>
<p>Your total points are: <input type="text" name="grossPts" size="5" /></p>
</form>
</body>
</html>
The first error is on this line:
<input type="button" name="toClick" value="calculate" onclick="grossPts.value
= calcpts(points)" >
there is not a points
variable declared so it throws an error, you need to calculate it or pass a number, you're not using the variable inside your function anyways.
The second error is that parseint()
does not exist, change it for parseInt()
A third error is inside your function, your using a pay
variable that does not exist, you need to declare the variables in order to be able to use them. But i guess you meant to write pts
instead of pay
Here's a gift, because why not?:
function calcpts(points) { if (window.document.jsquiz.rad1[0].checked == true) { var pts = 0; pts = pts + parseInt(20); alert(pts); return pts; } }
<h1>JavaScript Quiz</h1> <form name = "jsquiz"> <p>Question #1 You can test a condition with an if..else statement or with an if..elseif..else statement</p> <input type = "radio" name = "rad1" value="ques">True<br> <input type = "radio" name = "rad1" value="ques">False<br><br> <input type="button" name="toClick" value="calculate" onclick="grossPts.value = calcpts(0)" > </p> <br> <p>Your total points are: <input type="text" name="grossPts" size="5" /></p> </form>
在代码中用parseInt
替换函数parseint
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.