Dear forum users, there is a test code (code below) which takes the values ​​of a block consisting of <input type="radio"> with the same id. It checks which value is correct and if the item with value=1 selected, then it considers the question correctly answered. But I needed to make several answers. But so that if at least one of them is wrong, then the whole question is considered wrong. That is, if you need to choose 3 options, then only the answer was counted as correct, with these 3 options answered. Can you help implement JS check?

 function count() { answer = 0; answerBlocksCount = document.form1.getElementsByTagName("p").length; questionsCount = document.form1.querySelectorAll('input[type=radio]').length + 1; var ball = 0; var good = ""; var bad = ""; var solv = 1; for (var i = 0; i < questionsCount; i++) { if (document.form1.elements[i].checked) { if (document.form1.elements[i].value != 0) { if (good != "") { good += ", " + solv; } else { good += "№ " + solv; } } else { if (bad != "") { bad += ", " + solv; } else { bad += "№ " + solv; } } solv++; } } for (var i = 0; i < questionsCount; i++) { if (document.form1.elements[i].checked) { answer += 1; } } if (answer < answerBlocksCount) { alert("Вы не ответили на все вопросы теста!"); } else { if (good == "") good = " нет :("; if (bad == "") bad = " нет =)"; var answer = "<span style=color:green>Решены правильно:</span> " + good + "<br>"; if (bad == "нет") { answer += "Неправильно: " + bad + "."; } else { answer += "<span style=color:red>Неправильно:</span>" + bad + ""; } if (navigator.userAgent.toLowerCase().indexOf("gecko") > 0) { div1 = document.getElementById('div1'); } div1.innerHTML = answer; div1.style.display = 'block'; var paragraphs = document.form1.getElementsByTagName("i").length; for (s = 0; s < paragraphs.length; s++) { if (document.form1.elements[s].checked) { paragraphs(s).style.fontWeight = 'bold'; if (document.form1.elements[s].value != 0) { paragraphs(s).style.color = 'green'; } else { paragraphs(s).style.color = 'red'; } } } } } 
 <form name="form1" class="test"> <p>1. Вопрос 1 <br> <input type="radio" name="a" value="1"><i>Вариант 1</i> <br> <input type="radio" name="a" value="0"><i>Вариант 2</i> <br> <input type="radio" name="a" value="0"><i>Вариант 3</i> <br> <input type="radio" name="a" value="0"><i>Вариант 4</i> <br> <input type="radio" name="a" value="0"><i>Вариант 5</i> </p> <p>2. Вопрос 2 <br> <input type="radio" name="b" value="0"><i>Вариант 1</i> <br> <input type="radio" name="b" value="0"><i>Вариант 2</i> <br> <input type="radio" name="b" value="0"><i>Вариант 3</i> <br> <input type="radio" name="b" value="1"><i>Вариант 4</i> <br> <input type="radio" name="b" value="0"><i>Вариант 5</i> </p> <p>3. Вопрос 3 <br> <input type="radio" name="c" value="0"><i>Вариант 1</i> <br> <input type="radio" name="c" value="1"><i>Вариант 2</i> <br> <input type="radio" name="c" value="0"><i>Вариант 3</i> <br> <input type="radio" name="c" value="0"><i>Вариант 4</i> <br> <input type="radio" name="c" value="0"><i>Вариант 5</i> </p> <div border="1" id="div1" style="padding:9px 0 11px 10px;margin:9px 17px;border:1px solid green;display:none;"></div> <input type="button" value="Узнать результат" onclick="count();"> </form> 

    1 answer 1

    Well, firstly you should replace all the radio on the checkbox . so it will be easier to put more than one answer. How to check the answers and in general the presentation of answers is at your discretion:

    1. You can simply make an array, where each element is another array that describes the answer to one question (for example, var otv[] = [1,3,5]; var massOtv=[otv,otv,...] ) and then when checking one block of checkboxes, compare the selected elements with an array.

    Type if (massOtv[j][k] == checkbox[i].val()) where i and j and l are different counters because There are different number of answer options and the answers themselves (that is, j is read to massOtv.length - that is, this is your question number, k is a search of answer options). Validation - if the number of matched answers is equal to massOtv [j] .length, then the answer is correct.

    2. It is possible and otherwise - you can make the array in advance for the answer "full length" if only 5 response options are assumed, then massOtv[5] array massOtv[5] . In it, the correct answer options can be marked as 1, the wrong ones - 0, and also run during the test.

    3. The answer is in the form of the string "2; 3; 5", which is easier to store, but it is more difficult to parse (i.e., before checking, either 1) "fold" the response options from the checkboxes into the string or 2) parse the response string for convenient comparisons. )

    The check depends on the chosen “style” of your response array. If I described a little "difficult", then write what exactly is not clear and I will try to supplement the answer.