mercredi 26 octobre 2016

Quiz percentage validation IF - THEN functions

I am working to create a quiz. and I want to add the option for all the people that has 60% or more to have a congratilation message and to show a link to another web page were they can go and fill a form. For 60% or less just to show a sorry message.

Below is my sample code for my quiz. (this has only 2 questions FYI)

Thanks :

var numQues = 2;
var numChoi = 3;

var answers = new Array(2);
answers[0] = "Answer 1-1";
answers[1] = "Answer 2-1";

function getScore(form) {
  var score = 0;
  var currElt;
  var currSelection;

  for (i=0; i<numQues; i++) {
        currElt = i*numChoi;
        for (j=0; j<numChoi; j++) {
          currSelection = form.elements[currElt + j];
          if (currSelection.checked) {
                if (currSelection.value == answers[i]) {
                  score++;
                  break;
                }
          }
        }
  }

  score = Math.round(score/numQues*100);
  form.percentage.value = score + "%";

  var correctAnswers = "";
  for (i=1; i<=numQues; i++) {
        correctAnswers += i + ". " + answers[i-1] + "\r\n";
  }
  form.solutions.value = correctAnswers;

}
<form name="quiz">
1. Question 1<br>
<input type="radio" name="q1" value="Answer 1-1">Answer 1-1<br>
<input type="radio" name="q1" value="Answer 1-2">Answer 1-2<br>
<input type="radio" name="q1" value="Answer 1-3">Answer 1-3<br>
<p>

2. Question 2<br>
<input type="radio" name="q2" value="Answer 2-1">Answer 2-1<br>
<input type="radio" name="q2" value="Answer 2-2">Answer 2-2<br>
<input type="radio" name="q2" value="Answer 2-3">Answer 2-3<br>
<p>

<input type="button" value="Get score" onClick="getScore(this.form)">
<input type="reset" value="Clear"><p>
Score = <input type=text size=15 name="percentage"><br>
Correct answers:<br>
<textarea name="solutions" wrap="virtual" rows="4" cols="40"></textarea>
</form>

Aucun commentaire:

Enregistrer un commentaire