/    Sign up×
Community /Pin to ProfileBookmark

Submit answer for quiz

[CODE]<html>
<body>
<form name=”myForm”>
Question 1: Which of these is a letter?<br>
<input type=”radio” name=”q1″ value=”correct”>A<br>
<input type=”radio” name=”q1″>1<br>
<input type=”radio” name=”q1″>#<br>
<input type=”radio” name=”q1″>None of the Above<br>
<br>
<br>
Question 2: Which of these is a number?<br>
<input type=”radio” name=”q2″>A<br>
<input type=”radio” name=”q2″ value=”correct”>1<br>
<input type=”radio” name=”q2″>#<br>
<input type=”radio” name=”q2″>None of the Above<br>
<br>
<br>
Question 3: Which of these is a symbol?<br>
<input type=”radio” name=”q3″>A<br>
<input type=”radio” name=”q3″>1<br>
<input type=”radio” name=”q3″ value=”correct”>#<br>
<input type=”radio” name=”q3″>None of the Above<br>
<br>
<br>
Question 4: Which of these is a comma?<br>
<input type=”radio” name=”q4″>A<br>
<input type=”radio” name=”q4″>1<br>
<input type=”radio” name=”q4″>#<br>
<input type=”radio” name=”q4″ value=”correct”>None of the Above<br>
<br>
<br>

<a href=”#” onclick=”return checkAnswers()”>Get Certificate</a>

</form>
</body>

</html>
<script language=”JavaScript”>

var allQuestions = new Array(document.myForm.q1,
document.myForm.q2,
document.myForm.q3,
document.myForm.q4);

function checkAnswers(){

var totalScore = 0;

for (var i = 0; i < allQuestions.length; i++) {

if (allQuestions[i].value == “correct” && allQuestions[i].checked == true) {
totalScore++;

}

}

if ((totalScore/allQuestions.length) >= .75) {

alert(“Congratulations! Your score of ” + totalScore +
” out of ” + allQuestions.length + ” is good enough to proceed!”);

window.location.href = “certificate.html”;
}
else{

alert(“You must get at least 75% correct to move on!”);
return false;
}
}
</script>[/CODE]

I think i have some problems with var totalScore. The code don’t process on to certification.html page. Can someone help ?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@TheAliveWinnerApr 30.2012 — certification.html or certificate.html?
Copy linkTweet thisAlerts:
@stevengohauthorApr 30.2012 — certificate.html, I think i have problem with the for loop. It doesn't add the totalScore++;
Copy linkTweet thisAlerts:
@PadonakApr 30.2012 — [CODE]<!DOCTYPE html>
<html>
<body>
<form name="myForm">
Question 1: Which of these is a letter?<br>
<input type="radio" name="q1" value="correct">A<br>
<input type="radio" name="q1">1<br>
<input type="radio" name="q1">#<br>
<input type="radio" name="q1">None of the Above<br>
<br>
<br>
Question 2: Which of these is a number?<br>
<input type="radio" name="q2">A<br>
<input type="radio" name="q2" value="correct">1<br>
<input type="radio" name="q2">#<br>
<input type="radio" name="q2">None of the Above<br>
<br>
<br>
Question 3: Which of these is a symbol?<br>
<input type="radio" name="q3">A<br>
<input type="radio" name="q3">1<br>
<input type="radio" name="q3" value="correct">#<br>
<input type="radio" name="q3">None of the Above<br>
<br>
<br>
Question 4: Which of these is a comma?<br>
<input type="radio" name="q4">A<br>
<input type="radio" name="q4">1<br>
<input type="radio" name="q4">#<br>
<input type="radio" name="q4" value="correct">None of the Above<br>
<br>
<br>

<a href="#" onclick="return checkAnswers()">Get Certificate</a>

</form>

<script language="JavaScript">
function checkAnswers(){
var totalScore = 0;
var elems = document.forms['myForm'].elements;
var correct_answers = 0;
for(var i = 0; i < elems.length; i++){
if(elems[i].value == "correct"){correct_answers++;}
if(elems[i].value == "correct" && elems[i].checked == true){totalScore++;}
}
if((totalScore*100/correct_answers) >= 75){
alert("Congratulations! Your score of " + totalScore +" out of " + correct_answers + " is good enough to proceed!");
location.href = "certificate.html";
}
else{alert("You must get at least 75&#37; correct to move on!");return false;}
}
</script>
</body>
</html>[/CODE]
×

Success!

Help @stevengoh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...