-1

hi still new to javascript and w3 schools is hurting my head. could some one help me to add 1 to score if getanswer1 is equal to answer[0]

ive seen a million tutorials on making a quiz but they don't seem to explain it (they just give the code which is doesn't help at all).

HTML

<h2 id="numberofquestions"></h2>
<h3 id="q1"></h3>
        <form class="answers" name="question1form">
        <input type="radio" onclick = "getAnswers()" value="a" name="question1"><label class="pAnswers">a) .</label>
        <input type="radio" onclick = "getAnswers()" value="b" name="question1"><label class="pAnswers">b) .</label>
        <input type="radio" onclick = "getAnswers()" value="c" name="question1"><label class="pAnswers">c) .</label>
        <input type="radio" onclick = "getAnswers()" value="d" name="question1"><label class="pAnswers">d) .</label>
        </form>

<h3 id="q2"></h3>
    <form class="answers" name="question2form">
        <input type="radio" onclick = "getAnswers()" value="a" name="question2"><label class="pAnswers">a) .</label>
        <input type="radio" onclick = "getAnswers()" value="b" name="question2"><label class="pAnswers">b) .</label>
        <input type="radio" onclick = "getAnswers()" value="c" name="question2"><label class="pAnswers">c) .</label>
        <input type="radio" onclick = "getAnswers()" value="d" name="question2"><label class="pAnswers">d) .</label>
    </form>

Javascript

console.log("Woop js is linked and working" +
" P.s 42!");

var noOfQuestions = 10;
var score = 0;
var questions = ["question 1", "question 2", "question 3", "question 4", "question 5", "question 6", "question 7", "question 8", "question 9", "question 10"];
var answer = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b'];


document.getElementById('numberofquestions').innerHTML = ("this quiz has ") + noOfQuestions + ("  questions please answer them all");

function displayQuestions() {

        document.getElementById('q1').innerHTML = questions[0];
        document.getElementById('q2').innerHTML = questions[1];
        document.getElementById('q3').innerHTML = questions[2];
        document.getElementById('q4').innerHTML = questions[3];
        document.getElementById('q5').innerHTML = questions[4];
        document.getElementById('q6').innerHTML = questions[5];
        document.getElementById('q7').innerHTML = questions[6];
        document.getElementById('q8').innerHTML = questions[7];
        document.getElementById('q9').innerHTML = questions[8];
        document.getElementById('q10').innerHTML = questions[9];

}

// if input equals answer [letter] then add to score

function getAnswers() {

        var getAnswer1 = document.getElementsByName('question1');
        var getAnswer2 = document.getElementsByName('question2');
        var getAnswer3 = document.getElementsByName('question3');
        var getAnswer4 = document.getElementsByName('question4');
        var getAnswer5 = document.getElementsByName('question5');


        if ( getAnswer1.radioValue ('a')) {
                // radio == answer[0] then ++1 score else do nothing
                // $.inArray( getAnswer1, [answer[0]] );

        }

}

thanks for any help

octo-carrot
  • 240
  • 1
  • 11

1 Answers1

1

It seems to me the main body of your question is "How do I get the value of a radio input", which is answered here: Get Radio Button Value with Javascript . The rest is just checking the value against the correct answer:

if (getRadioValue(getAnswer1) === answer[0]) {
    ++score;
}

supposing you put the above-linked solution into a getRadioValue() function like this:

function getRadioValue(radios) {
    for (var i = 0, length = radios.length; i < length; i++) {
        if (radios[i].checked) {
            return radios[i].value;
        }
    }
}

Offtopic hint: Looking at your code, I'd suggest learning more about basic language constructs (in this case, cycles) before venturing forward into creating interactive forms.

Community
  • 1
  • 1
hon2a
  • 7,006
  • 5
  • 41
  • 55