2015-01-21 164 views
-2

所以我試圖做一個quiz.html文件,它會提示用戶輸入no。他們想回答的問題。根據他們的意見,會產生隨機問題。所以我明白它必須是一個循環,直到不循環。輸入到警報框中。
所以我的scoreMe()函數顯然沒有給出任何答案。我試圖檢查一個特定問題的選中單選按鈕是否等於正確的答案,但我只是得到了0%的分數。那麼誰能告訴我我的代碼有什麼問題?在javascript中做一個隨機測驗

 <div id="quesRes"></div> 
<script> 

    var quizObj = [ 
    { 
     "question": "What is the capital of Bangladesh?", 
     "choice": ["Dhaka", "Chittagong", "Sylhet"], 
     "correct": ["Dhaka"] 
    }, 
    { 
     "question": "What does 2+2 equal to?", 
     "choice": ["3", "2", "4"], //quizObj[2].choice[0],quizObj[2].choice[1] 
     "correct": ["4"] 
    }, 
    { 
     "question": "What is the real name of Damon Salvatore?", 
     "choice": ["Paul Wesley", "Steven McQueen", "Ian Somerhalder"], //quizObj[2].choice[0],quizObj[2].choice[1] 
     "correct": ["Ian Somerhalder"] 
    }, 
    { 
     "question": "What is the name of the largest planet in the universe?", 
     "choice": ["Earth", "Jupiter", "Uranus"], //quizObj[2].choice[0],quizObj[2].choice[1] 
     "correct": ["Jupiter"] 
    }, 
    { 
     "question": "What is the capital of New York?", 
     "choice": ["Manhattan", "NYC", "Albany"], //quizObj[2].choice[0],quizObj[2].choice[1] 
     "correct": ["Albany"] 
    }, 
    { 
     "question": "How many bones does the human body have?", 
     "choice": ["109", "206", "114"], //quizObj[2].choice[0],quizObj[2].choice[1] 
     "correct": ["206"] 
    }, 
    { 
     "question": "What is the alter ego of Batman?", 
     "choice": ["Bruce Banner", "Bruce Wayne", "Tony Stark"], //quizObj[2].choice[0],quizObj[2].choice[1] 
     "correct": ["Bruce Wayne"] 
    }, 
    { 
     "question": "How many books are there in the Harry Potter series?", 
     "choice": ["7", "5", "8"], //quizObj[2].choice[0],quizObj[2].choice[1] 
     "correct": ["7"] 
    }, 
    { 
     "question": "What is Naruto's surname?", 
     "choice": ["Sarutobi", "Uchiha", "Uzumaki"], //quizObj[2].choice[0],quizObj[2].choice[1] 
     "correct": ["Uzumaki"] 
    }, 
    { 
     "question": "What is the name of Sherlock Holmes' partner?", 
     "choice": ["Peterson", "Watson", "Hanson"], //quizObj[2].choice[0],quizObj[2].choice[1] 
     "correct": ["Watson"] 
    }, 

]; 

    var track = []; 

    var maxQues = prompt("How many questions do you want to answer?", "5"); 
    var rand = Math.floor(Math.random() * maxQues); 

    var str='<h4>Answer all the questions</h4>'; 

    for(var i=0;i<maxQues;i++){ 

     var rand = Math.floor(Math.random() * maxQues); 
     str+=(i+1)+'. '+quizObj[rand].question+'<br>'; 
     str+='<form><table>'+ 
      '<tr><td id="a1"><input type="radio" name="radio' + i + '"/>'+'&nbsp;&nbsp;'+quizObj[rand].choice[0]+'</td></tr>'+ 
      '<tr><td id="a2"><input type="radio" name="radio' + i + '" />'+'&nbsp;&nbsp;'+quizObj[rand].choice[1]+'</td></tr>'+ 
      '<tr><td id="a3"><input type="radio" name="radio' + i + '"/>'+'&nbsp;&nbsp;'+quizObj[rand].choice[2]+'</td></tr>'+ 
     '</table></form><br>'; 
    track[i]=rand; 
    } 
    str += '<input value="Submit" type="button" onclick="scoreMe()"><br><br>'; 
    str += 'Score: <input id="score" type="text" size="8" ><br><br><br>'; 
    document.getElementById('quesRes').innerHTML = str; 

    function scoreMe(){ 
    var sum=0; 
    for(var j=0;j<maxQues;j++){ 
     for(var k=0;k<3;k++){ 
     if(quizObj[track[j]].choice[k].checked===quizObj[track[j]].correct[0]){ 
      console.log('Works'+j); 
      sum++; 
     } 
     } 
    } 
    document.getElementById('score').value = ((sum/maxQues)*100)+'%'; 
    } 
</script> 
+0

@BlackPOP:審查修改時請注意。從'quiz.html'到'測驗的編輯。 HTML'確實無效。 – Cerbrus 2015-01-21 11:39:27

回答

0

所有的單選按鈕具有相同的名稱,因此屬於同一組。 給他們不同的名字,你應該罰款:

str+='<table>'+ 
      '<tr><td id="a1"><input type="radio" name="radio' + i + '" />'+'&nbsp;&nbsp;'+quizObj[rand].choice[0]+'</td></tr>'+ 
      '<tr><td id="a2"><input type="radio" name="radio' + i + '" />'+'&nbsp;&nbsp;'+quizObj[rand].choice[1]+'</td></tr>'+ 
      '<tr><td id="a3"><input type="radio" name="radio' + i + '"/>'+'&nbsp;&nbsp;'+quizObj[rand].choice[2]+'</td></tr>'+ 
     '</table><br>'; 

Quoting MDN on the subject

有name屬性相同的值是相同的「單選按鈕組」中

單選按鈕;一次只能選擇一個組中的一個單選按鈕。

0

替換這些行:

'<tr><td id="a1"><input type="radio" name="radio" />'+'&nbsp;&nbsp;'+quizObj[rand].choice[0]+'</td></tr>' 

有了:

'<tr><td id="a1"><input type="radio" name="radio' + i + '" />'+'&nbsp;&nbsp;'+quizObj[rand].choice[0]+'</td></tr>' 
//           There^

這樣,輸入端的 「名」(這是用來 「羣」 它們),有問題的身份證號碼。

0

,而不是使用「字符串」作爲回答您的問題,添加索引號..它會解決這個問題

var quizObj = [ 
{ 
    "question": "What is the capital of Bangladesh?", 
    "choice": ["Dhaka", "Chittagong", "Sylhet"], 
    "correct": 0