I
I
Irina Gulevich2018-08-30 06:47:54
JavaScript
Irina Gulevich, 2018-08-30 06:47:54

The test script does not work correctly, why does it display an incorrect test result?

There is a distance learning test script
like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<meta http-equiv="Title" content="Тест" />
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251" />

<script type="text/javascript">
var d=document, rez=0, 
    time='60',         //общее время
    ball=0,            //балл за вопрос
    vsego=5,           //всего вопросов
    count=0;           //Кол-во правильных ответов


function timer(){
var obj=document.getElementById('timer');

obj.innerHTML--;
    
if(obj.innerHTML==0){

stopTest();
setTimeout(function(){},1000);}

else{setTimeout(timer,1000);}

}


function startTest(){
d.getElementById("timer").innerHTML=time;   
setTimeout(timer,1000);
d.getElementById("blok").style.display='block';
d.getElementById("btStart").style.display='none';

}

function stopTest(){

var i=0;

//Первый вопрос (переключатели)
for(i=0; i<d.getElementsByName("q1").length; i++){
    
    if(d.getElementsByName("q1")[i].checked) {
      
      if(i==1){rez+=2; count++;}      //какой правильный чекбокс, начинается с 0 
    }
}



//Второй вопрос (чекБоксы)
for(i=0; i<d.getElementsByName("q2").length; i++){

    if(d.getElementsByName("q2")[i].checked) {
      
      if(i==0){rez+=2; count++;}      //какой правильный чекбокс, начинается с 0 
      if(i==2){rez+=2; count++;}
    }

}


//Третий вопрос (выподающий список)
for(i=0; i<d.getElementById("q3").options.length; i++){

    if(d.getElementById("q3").options[i].selected){
        
        if(i==2){rez+=2; count++;}    //правильная строка - начинается с  0 
    }  
}

//Четвертый вопрос (текстовая строка)
if(d.getElementsByName("q4")[0].value.search("Привет")!=-1){rez+=2; count++;}

//Пятый вопрос (текстовая строка)
if(d.getElementsByName("q5")[0].value.search("ололол")!=-1){rez+=2;count++;}


alert('Вы набрали '+((rez/ball)/vsego)*100+' %\nВы правильно ответили на '+count+ ' вопросов из '+vsego+'.');         

d.getElementById("blok").style.display='none';
d.getElementById("btStart").style.display='block';
location.reload(true);
rez=0;
count=0;
}

</script>


  <title>Тест</title>
 
 <style type="text/css">
<!--
#blok{
display: none;
}

-->
</style>   
    
</head>

<body>

<input type="button" name="btStart" value="Старт тест" onclick="startTest()" id="btStart"/>

<div id="blok">
<p style="float:left;">Осталось времени:&nbsp;</p>
<p id="timer"></p>

<form method="get" enctype="text/plain" name="testForm">
<div id="blockQuest">
<p class="quest">Вопрос 1</p>
<input type="radio" name="q1" value="1" />ответ1<br />
<input type="radio" name="q1" value="2" />ответ2<br />
<input type="radio" name="q1" value="3" />ответ3<br />
</div>

<div id="blockQuest">
<p class="quest">Вопрос 2</p>

<input type="checkbox" name="q2" value="1" />ответ 1<br />
<input type="checkbox" name="q2" value="2" />ответ 2<br />
<input type="checkbox" name="q2" value="3" />ответ 3<br />


</div>


<div id="blockQuest">
<p class="quest">Вопрос 3</p>

<select name="q3" id="q3">
  <option value="1">название 1</option>
  <option value="2">название 2</option>
  <option value="3">название 3</option>
  <option value="4">название 4</option>
</select>

</div>


<div id="blockQuest">
<p class="quest">Вопрос 4</p>
<input type="text" name="q4" />
</div>
<div id="blockQuest">
<p class="quest">Вопрос 5</p>
<input type="text" name="q5" />
</div>

<input type="button" value="Принять ответ" name="ok" onclick="stopTest()"/>

<input type="reset" value="Сбросить" name="res" />
</form>
</div>
</body>
</html>

After editing, it works correctly on the site. I took this script and edited it just like I open it in the editor for the first time. Stopped working correctly. I installed the same error on the site, it does not correctly calculate the test result. The script with the first edition continues to work correctly. I like this code, the tasks are not large, it is convenient to edit, I would like to do a few more tests based on this script. Who knows what is the reason, please help!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question