Click here to Skip to main content
15,851,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

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;
		}


What I have tried:

Code:

<!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>

<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])correct+=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>

</body>
</html>
Posted
Updated 12-Nov-22 20:56pm
v2

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900