You access the form elements before they exist
You missed a curly bracket before the else. I eliminated it.
<script type="text/javascript>
function validate(theForm) {
//put all the question sets into this array
var allQuestions = new Array(theForm.q1,
theForm.q2 /*,
theForm.q3,
theForm.q4 */);
return checkAnswers(allQuestions);
}
//redirects if 75% or greater, returns false otherwise
function checkAnswers(allQuestions){
var totalScore = 0; //initialize to 0
//go through each question set
for (var i in allQuestions) {
var temp = allQuestions[i];
//go through each radio button in the current question set
for (var j = 0; j < temp.length; j++) {
//if the correct one is chosen then add 1 to total score
if (temp[j].value == "correct" && temp[j].checked == true) {
totalScore++;
}
}
}
//if the total percentage is more than 75%
if ((totalScore/allQuestions.length) >= .75) {
//alert and move on
alert("Congratulations! Your score of " + totalScore +
" out of " + allQuestions.length + " is good enough to proceed!");
return true; // this will submit the form. return false if you do not want to submit at all
}
//otherwise alert and return false
alert("You must get at least 75% correct to move on!");
return false;
}
</script>
<h3>Wine Quiz</h3>
<form name="Quiz" onsubmit="return validate(this)">
.
.
<input type="submit" />
</form>