2016-05-16 3 views
0

JavaScriptでミニクイズを作成しようとしています。現在、質問に正しく答えると、クイズがアラートを出します。ユーザーに正しいことを通知するだけでなく、同じアラートウィンドウに次の質問、つまり「この質問に正しく回答しました。次の質問です...」JSクイズを作成すると、アラートウィンドウは次の質問をする代わりに以前のQを繰り返します。

ただし、次の質問をするのではなく、以前の質問を繰り返すだけで、正しく答えられました。以下は私のコードです:

var questions = [ 
{ 
question: "What is the capital of England?", 
answer: "LONDON", 
}, 

{ 
question: "What is the capital of France?", 
answer: "PARIS", 
}, 

{ 
question: "What is the capital of Germany?", 
answer: "BERLIN", 
} 
]; 

var correctAnswers = 0; 

for (var i = 0; i < questions.length; i++) { 
question = questions[i].question; 
answer = questions[i].answer; 
response = prompt(question); 
if (response.toUpperCase() === answer){ 
alert("Correct! " + questions[i].question); 
correctAnswers ++; 
} 
else { 
alert("Incorrect. The answer was " + questions[i].answer); 
} 
} 

html = correctAnswers + " correct answers." 
document.write(html); 

私は間違っていますか?

答えて

0

alertは次の質問をする必要があります。だから、ありがとうi+1

alert("Correct! " + questions[i+1].question); 

var questions = [{ 
 
    question: "What is the capital of England?", 
 
    answer: "LONDON", 
 
    }, 
 

 
    { 
 
    question: "What is the capital of France?", 
 
    answer: "PARIS", 
 
    }, 
 

 
    { 
 
    question: "What is the capital of Germany?", 
 
    answer: "BERLIN", 
 
    } 
 
]; 
 

 
var correctAnswers = 0; 
 
var response=""; 
 
for (var i = 0; i < questions.length; i++) { 
 
    question = questions[i].question; 
 
    answer = questions[i].answer; 
 
    response = response!=""?prompt("Correct! "+question):prompt(question); 
 
    if (response.toUpperCase() === answer) { 
 
    correctAnswers++; 
 
    if(i==questions.length-1) 
 
     alert("Correct"); 
 
    } else { 
 
    alert("Incorrect. The answer was " + questions[i].answer); 
 
    } 
 
} 
 
html = correctAnswers + " correct answers." 
 
document.body.innerHTML=html;

+0

を行います!そのコードは役に立ちましたが、警告が次の質問をしたときに、ユーザーが回答を入力するためのスペースがありませんでした。彼らは「OK」をクリックしなければならず、質問が繰り返される別のアラートウィンドウが現れます。今回は答えるスペースがあります。どのように私はこれを修正することができる任意のアイデア? – JSJunkie

+0

答えを更新しました。確認してください... –

関連する問題