1

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>
2
  • What is your intention with parseint(20)? If you wan to use the number 20, then just write 20. parseInt is for extracting a number from a string. Commented Apr 30, 2015 at 5:30
  • Where the "pay" variable comes from?? Commented Apr 30, 2015 at 5:32

2 Answers 2

1

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>

Sign up to request clarification or add additional context in comments.

Comments

0

Replace function parseint with parseInt in your code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.