2015-09-27 27 views
1
<script type="text/javascript"> 

var questions = [ 
    ['http://i.imgur.com/k0fBtYP.png','0'], 
    ['http://i.imgur.com/1PwDTVY.png','1'], 
    ['http://i.imgur.com/QKE9UFA.png','2'], 
    ['http://i.imgur.com/XEGhtgB.png','3'], 
    ['http://i.imgur.com/QhnCkAp.png','4'], 
    ['http://i.imgur.com/JoL8tco.png','5'], 
    ['http://i.imgur.com/tcZNls4.png','6'], 
    ['http://i.imgur.com/V9DQI0p.png','7'], 
    ['http://i.imgur.com/39ePipM.png','8'], 
    ['http://i.imgur.com/16yFeMy.png','9'], 
    ['http://i.imgur.com/UUo2yNc.png','10'], 
    ['http://i.imgur.com/5sza6Wm.png','11'], 
    ['http://i.imgur.com/ygPZBdY.png','12'], 
    ['http://i.imgur.com/SwJYBRR.png','13'], 
    ['http://i.imgur.com/wNdpJBX.png','14'], 
    ['http://i.imgur.com/wUS7pDs.png','15'], 
    ['http://i.imgur.com/OEI6ZYX.png','16'] 
]; 
var qNo = 0; 
var correct = 0; 
var cnt = 0; 

function NextQuestion(response) { 
    if ((qNo < questions.length) && (response == questions[qNo][1])) { correct++; } 
    document.getElementById('score').innerHTML 
    = 'Your score is '+correct+'/'+(cnt+1); 
    qNo++; 
    if (qNo < questions.length) { document.getElementById('Pic').src = questions[qNo][0]; cnt++; } 
        else { alert('Quiz is done'); } 
} 
onload = function() { 
document.getElementById('Pic').src = questions[0][0]; 
} 

</script> 

好吧,所以基本上我有17個問題和17個答案被列出使用數字0-16,我想要做的是隨機化的圖片顯示的順序,所以你不能找到一個模式,而不是回答它的權利,也是我無法弄清楚這一點,我想這樣做,以便在每個問題被回答後綠色正確或紅色不正確顯示取決於如果你有前面的問題對任何人都可以幫助?我如何隨機顯示我的問題,使其現在始終如一? (JavaScript)

+2

利用[Math.random](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random)。 – Nonemoticoner

+0

通常情況下,您會從數組中隨機拼接一個成員,直到沒有剩下任何成員。或者,如果您不想修改原始數組,您可以爲其構建* question *和splice成員的隨機索引。突出顯示正確或錯誤的答案可以通過添加或刪除類值來完成。 – RobG

+0

那麼,如何將它實現到我的代碼是我正在得到的。 –

回答

0
週期

看看this。一旦得到介於0和16之間的值,將其存儲爲i,然後顯示與i對應的問題。在得到答案後,從questions.splice(i, i+1);answers.splice(i, i+1);中刪除該問題並從數組中得到答案。現在對每個問題重複此操作,最終,問答數組的長度將爲0.這意味着用戶回答了所有問題。

+0

謝謝我明白了:D –