2015-06-15 54 views
1

所以我想爲我的地理課上做一個javascript遊戲,但我遇到了一些麻煩,我可以問問題,並告訴你如果你錯了或不是,但我想能夠跟蹤錯誤的答案。我想跟蹤使用for循環,但我不擅長他們,一些幫助將不勝感激!理解Javascript遊戲的不正確答案

這是一個什麼樣的每一個問題看起來像基礎,它只是& &是,我需要一個標記到不正確理貨我確信我需要使用for循環進行。

var y = "You are correct!!!" 
var n = "You are incorrect!!!" 

alert("Chapter 1, Human Cultural Connections. 1-10") 
//================================================== 
var Q1 = prompt("Demographers identify three different stages of life. 
They are children, working adults, and older adults. What is the age 
range for children? 0-13, 0-15, 0-18") 

if (Q1 === "0-13") 
{ 
alert(y) 
} 
else 
{ 
alert(n) //&& add 1 tally to incorrect list 
} 

如果有人可以幫助我這個這將是SOOO有幫助的,不要擔心,這是過去做的反正,但我還是想知道如何做到這一點爲將來的項目!

p.s.我已經有腳本HTML,所以我不需要幫助。

+0

[** This **](http://www.javascriptsource.com/miscellaneous/basic-javascript-quiz.html)可能會有所幫助。 – chridam

+1

'alertCreateCount = 0;'在頂部,'correctCount ++''alert(y)'後面怎麼樣? –

回答

1

添加一個變量來跟蹤不正確的答案:

var y = "You are correct!!!" 
var n = "You are incorrect!!!" 
var incorrectCount = 0; 

alert("Chapter 1, Human Cultural Connections. 1-10") 
//================================================== 
var Q1 = prompt("Demographers identify three different stages of life. 
They are children, working adults, and older adults. What is the age 
range for children? 0-13, 0-15, 0-18") 

if (Q1 === "0-13") 
{ 
alert(y) 
} 
else 
{ 
alert(n) //&& add 1 tally to incorrect list 
incorrectCount++; 
} 
+0

我會嘗試一下,看看會發生什麼 –

+0

它的功能完美無缺,我不需要做的就是在計數器擊中5時如何終止遊戲 –

2
var correct = [], // well store the index of the correctly answered questions here 
    wrong = [], // well store the index of the incorrectly answered questions here 
    questions = [ 
    { 
     "question": "Demographers identify three different stages of life. They are children, working adults, and older adults. What is the age range for children?", 
     "answers": ["0-13", "0-15", "0-18"], 
     "correct": 0 // correct answer is item of index 0 in property "answers" (0-13) 
    }, 
    { 
     "question": "whats your favorite color?", 
     "answers": ["red", "yellow", "blue", "purple"], 
     "correct": 2 // blue 
    } 
]; 

for (var i in questions){ 
    var answer = prompt(questions[i].question + questions[i].answers.join(',')); 
    if (answer == questions[i].answers[questions[i].correct]){ 
     correct.push(i); 
    }else{ 
     wrong.push(i); 
    }  
} 

alert('wrong number of answers: ' + wrong.length); 
alert('correct number of answers: ' + correct.length); 
alert('first wrong question: ' + questions[wrong[0]].question); 

我知道這實際上是overwngineering你問什麼,但它可能給你更好的靈活性和知識,以供循環JS如何工作。希望能幫助到你。