Это можно сделать проще, используя пример querySelector:
// Javascript (important I need a solution in this code)
function checkQuestion(questionName, answerID) {
if(questionName == null || answerID == null) return;
const answer = document.getElementById(answerID);
const question = document.querySelector(`input[name="${questionName}"]:checked`);
if(answer == null || question == null) return;
return answer.id === question.id;
}
function updateScore(name, score, isCorrect) {
if(isCorrect == null) return score;
if(isCorrect) {
document.querySelectorAll(`input[name="${name}"]:not(:checked)`).forEach(input => input.disabled = true);
return score + 1;
}
else alert("wrong answer");
return score;
}
function check() {
let score = 0;
// question no1
const nameQ1 = 'founder'
const validQ1 = checkQuestion(nameQ1, 'q1_o3');
score = updateScore(nameQ1, score, validQ1);
// question no2
const nameQ2 = 'national_poet';
const validQ2 = checkQuestion(nameQ2, 'q2_o1');
score = updateScore(nameQ2, score, validQ2);
// question no3
const nameQ3 = 'anthem_poet'
const validQ3 = checkQuestion(nameQ3, 'q3_o2');
score = updateScore(nameQ3, score, validQ3);
// question no4
const nameQ4 = 'national_animal'
const validQ4 = checkQuestion(nameQ4, 'q4_o4');
score = updateScore(nameQ4, score, validQ4);
// question no5
const nameQ5 = 'pm';
const validQ5 = checkQuestion(nameQ5, 'q5_o1');
score = updateScore(nameQ5, score, validQ5);
// question no6
const nameQ6 = 'pk_sq_km'
const validQ6 = checkQuestion(nameQ6, 'q6_o1');
score = updateScore(nameQ6, score, validQ6);
// question no7
const nameQ7 = 'first_PM';
const validQ7 = checkQuestion(nameQ7, 'q7_o1');
score = updateScore(nameQ7, score, validQ7);
// question no8
const nameQ8 = 'first_Capital';
const validQ8 = checkQuestion(nameQ8, 'q8_o1');
score = updateScore(nameQ8, score, validQ8);
// question no9
const nameQ9 = 'first_womanPM';
const validQ9 = checkQuestion(nameQ9, 'q9_o3');
score = updateScore(nameQ9, score, validQ9);
// question no9
const nameQ10 = 'founder2';
const validQ10 = checkQuestion(nameQ10, 'q10_o1');
score = updateScore(nameQ10, score, validQ10);
//display result
const ab = document.querySelector('.ab') || ((e) => {
e.classList.add('ab');
document.body.appendChild(e)
return e;
})(document.createElement("h3"));
ab.innerText = score;
}
/* *{
padding: 0px;
margin: 0px;
} */
body{
margin: 1em;
color: white;
background-color: #222;
background-image: url(images/bg.jpg);
background-repeat: no-repeat;
background-size: cover;
background-position: center;
}
fieldset{
border-radius: 15px;
text-align: center;
}
h3{
background-color: cornsilk;
color: blue;
width: 10vw;
height: 5vh;
text-align: center;
position: relative;
left: 45%;
}
input[type=radio]:disabled ~ label {
color: gray;
}
<h1>Quiz Application</h1>
<div>
<fieldset>
<p>Who is the founder of Pakistan?</p>
<input type="radio" name="founder" id="q1_o1"><label>Allama Iqbal</label>
<input type="radio" name="founder" id="q1_o2"><label>Manzoor Kapri</label>
<input type="radio" name="founder" id="q1_o3"><label>Quaid-e-Azam</label>
<input type="radio" name="founder" id="q1_o4"><label>Liaquat Ali Jinah</label>
</fieldset>
</div><br>
<br>
<br>
<div>
<fieldset>
<p>Who is national poet of Pakistan?</p>
<input type="radio" name="national_poet" id="q2_o1"><label>Allama Iqbal</label>
<input type="radio" name="national_poet" id="q2_o2"><label>Manzoor Kapri</label>
<input type="radio" name="national_poet" id="q2_o3"><label>Quaid-e-Azam</label>
<input type="radio" name="national_poet" id="q2_o4"><label>Liaquat Ali Jinah</label>
</fieldset>
</div><br>
<br>
<br>
<div>
<fieldset>
<p>Who is the writer of national anthem of Pakistan?</p>
<input type="radio" name="anthem_poet" id="q3_o1"><label>Allama Iqbal</label>
<input type="radio" name="anthem_poet" id="q3_o2"><label>Hafiz Jalindheri</label>
<input type="radio" name="anthem_poet" id="q3_o3"><label>Quaid-e-Azam</label>
<input type="radio" name="anthem_poet" id="q3_o4"><label>Liaquat Ali Jinah</label>
</fieldset>
</div><br>
<br>
<br>
<div>
<fieldset>
<p>what is the national animal of Pakistan?</p>
<input type="radio" name="national_animal" id="q4_o1"><label>Peacock</label>
<input type="radio" name="national_animal" id="q4_o2"><label>Dog</label>
<input type="radio" name="national_animal" id="<label>q4_o3"><label>Lion</label>
<input type="radio" name="national_animal" id="q4_o4"><label>Markhor</label>
</fieldset>
</div><br>
<br>
<br>
<div>
<fieldset>
<p>Who is the Prime Minister of Pakistan?</p>
<input type="radio" name="pm" id="q5_o1"><label>Imran Khan</label>
<input type="radio" name="pm" id="q5_o2"><label>Manzoor Kapri</label>
<input type="radio" name="pm" id="q5_o3"><label>Quaid-e-Azam</label>
<input type="radio" name="pm" id="q5_o4"><label>Liaquat Ali Jinah</label>
</fieldset>
</div><br>
<br>
<br>
<div>
<fieldset>
<p>what is the area of pakistan in square km?</p>
<input type="radio" name="pk_sq_km" id="q6_o1"><label>1,96,096 sq km</label>
<input type="radio" name="pk_sq_km" id="q6_o1"><label>5,05,095 sq km</label>
<input type="radio" name="pk_sq_km" id="q6_o1"><label>7,96,096 sq km</label>
<input type="radio" name="pk_sq_km" id="q6_o1"><label>2,96,096 sq km</label>
</fieldset>
</div><br>
<br>
<br>
<div>
<fieldset>
<p>Who is the first prime minister of Pakistan?</p>
<input type="radio" name="first_PM" id="q7_o1"><label>Liaquat Ali</label>
<input type="radio" name="first_PM" id="q7_o2"><label>Zulfiqar Ali Bhutto</label>
<input type="radio" name="first_PM" id="q7_o3"><label>Quaid-e-Azam</label>
<input type="radio" name="first_PM" id="q7_o4"><label>Manzoor Kapri</label>
</fieldset>
</div><br>
<br>
<br>
<div>
<fieldset>
<p>Which was the first capital of Pakistan?</p>
<input type="radio" name="first_Capital" id="q8_o1"><label>Karachi</label>
<input type="radio" name="first_Capital" id="q8_o2"><label>Islamabad</label>
<input type="radio" name="first_Capital" id="q8_o3"><label>Hydrabad</label>
<input type="radio" name="first_Capital" id="q8_o4"><label>Quetta</label>
</fieldset>
</div><br>
<br>
<br>
<div>
<fieldset>
<p>Who is the first woman prime minister of Pakistan?</p>
<input type="radio" name="first_womanPM" id="q9_o1"><label>Shoukat Khanam</label>
<input type="radio" name="first_womanPM" id="q9_o2"><label>Shamshad Akhtar</label>
<input type="radio" name="first_womanPM" id="q9_o3"><label>Benazir Bhutto</label>
<input type="radio" name="first_womanPM" id="q9_o4"><label>Liaquat Ali Jinah</label>
</fieldset>
</div><br>
<br>
<br>
<div>
<fieldset>
<p>which is the current capital of Pakistan?</p>
<input type="radio" name="founder2" id=""><label>Islamabad</label>
<input type="radio" name="founder2" id=""><label>Karachi</label>
<input type="radio" name="founder2" id=""><label>Hydrabad</label>
<input type="radio" name="founder2" id=""><label>Lahore</label>
</fieldset>
</div><br>
<br>
<br>
<button onclick="check()">Results</button>