• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Quiz with select form JS

Georg

New Coder
how can i make it take the correct answers from the select and add them to the correct. If answered correctly in the select to write "You got 7 correct".

Now it works only for radio button and for the text ...

I make for cycle but i dont know how to add the check of if to work properly

JavaScript:
var qs_select= document.getElementsByTagName("select");
    var num_select_questions=qs_select.length/1;
    var ans_select=[2,1];

for(let i=0; i<num_select_questions; i++){
            var left=qs_select[i];
            var right= qs_select[i+1];
            if(left.selectedIndex==ans_select[i] &&
                right.selectedIndex==ans_select[i+1])sum +=1;
        }


HTML

HTML:
<!DOCTYPE html>
<html>
<head>

<title>Document</title>
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
</head>

<body>

<form id = "quiz" name = "quiz">

<p class = "questions">"Who is write Elegiya" </p>
<input id = "textbox" type = "text" name = "question1">

<p class = "questions">Real name of Elin Pelin?</p>
<input id = "textbox" type = "text" name = "question5">

<p class = "questions">In whitch age Hristo Botev died?</p>
<input id = "textbox" type = "text" name = "question6">

<p class = "questions">Which Bulgarian rivers spring from the Rila mountain?</p>
<input type = "radio" id = "mc" name = "question2" value = "Iskar and Maritsa">Iskar and Maritsa<br>
<input type = "radio" id = "mc" name = "question2" value = "Augusta and Yantra">Augusta and Yantra<br>

<p class = "questions"> Which was the capital of Bulgaria during the reign of Khan Krum</p>

<input type = "radio" id = "mc" name = "question3" value = "Turnovo">Turnovo<br>
<input type = "radio" id = "mc" name = "question3" value = "Pliska">Pliska<br>

<p class = "questions">Which literary school was founded by Clement, the pupil of Cyril and Methodius?</p>
<input type = "radio" id = "mc" name = "question4" value = "Turnovska">Turnovska<br>
<input type = "radio" id = "mc" name = "question4" value = "Ohrid">Ohrid<br>

<p>Select your matching items from the two adjacent lists</p>
<fieldset  id="select">
    <div>
        <select size="3">   
            <option>Atanas Dalchev</option>
            <option>Hristo Botev</option>
            <option>Geo Milev</option>
        </select>
        <select size="3">   
            <option>To Chicago and Back</option>
            <option>September</option>
            <option>Albena</option>
        </select>
</fieldset>


<input id = "button" type = "button" value = "The moment of truth ... !" onclick = "check();">


</form>

<div id = "after_submit">
<p id = "number_correct"></p>
<p id = "message"></p>
<img id = "picture">
</div>

</body>
</html>




JS


JavaScript:
<script>
  
   function check(){

    var question1 = document.quiz.question1.value;
    var question2 = document.quiz.question2.value;
    var question3 = document.quiz.question3.value;
    var question4 = document.quiz.question4.value;
    var question5 = document.quiz.question5.value;
    var question6 = document.quiz.question6.value;
    var correct = 0;

    
    if (question1 == "Hristo Botev"){
        correct++;
    }
    if (question5 == "Dimitur Stoyanov"){
        correct++;
    }
    if (question6 == "28"){
        correct++
    }
    if (question2 == "Iskar and Maritsa"){
        correct++;
    }   
    if (question3 == "Pliska"){
        correct++;
    }
    if (question4 == "Oxrid"){
        correct++;
    }

var pictures = ["img/win.gif", "img/meh.jpeg", "img/lose.gif"];
var messages = ["Great job!", "That's just okay", "You really need to do better"];
var score;

if (correct == 0) {
    score = 2;
}

if (correct > 0 && correct < 6) {
    score = 1;
}

if (correct == 6) {
    score = 0;
}

// for select
var qs_select= document.getElementsByTagName("select");
    var num_select_questions=qs_select.length/1;
    var ans_select=[2,1];

for(let i=0; i<num_select_questions; i++){
            var left=qs_select[i];
            var right= qs_select[i+1];
            if(left.selectedIndex==ans_select[i] &&
                right.selectedIndex==ans_select[i+1])score+=1;
        }

document.getElementById("after_submit").style.visibility = "visible";

document.getElementById("message").innerHTML = messages[score];
document.getElementById("number_correct").innerHTML = "You got " + correct + " correct.";
document.getElementById("picture").src = pictures[score];
}

</script>
 

OldMan

King Coder
Don't think you need the first JS code to cycle. Cycling is not necessary with a submit button.
You have a div with no </div> in the <fieldset id="select">.
You have value = "Ohrid" in the HTML and if (question4 == "Oxrid"){ in the JS.
You have if (question1 == "Hristo Botev"){ BUT what if they just write in the last name, how about not capitalizing the name?
The question you asked is far too hard for you to code and would take more time to do it justice than I or other people here have, so think of a better way of doing this.
 

Georg

New Coder
Don't think you need the first JS code to cycle. Cycling is not necessary with a submit button.
You have a div with no </div> in the <fieldset id="select">.
You have value = "Ohrid" in the HTML and if (question4 == "Oxrid"){ in the JS.
You have if (question1 == "Hristo Botev"){ BUT what if they just write in the last name, how about not capitalizing the name?
The question you asked is far too hard for you to code and would take more time to do it justice than I or other people here have, so think of a better way of doing this.
thank you, i've done better version
 
Top