2016-05-05 30 views
1

私はループと配列が何であるかを知る前に、私は小型のJSクイズを書いていましたが、今はちょっと整理しようとしています。元のクイズは、アラートウィンドウでユーザーに質問し、誤って答えた場合、正しい答えが何であるかを示します。答えが正しい場合は、次の質問に進む前に、正しいことを伝えます。それはまた、ユーザーが正しいか間違っているかの回答の数を保持します。ここに私のオリジナルの(過剰な)コードがあります:配列とループを使ってJSクイズを書き直そうとすると正しい答えはどこに行きますか?

var correct = 0; 

var answer1 = prompt("What is the capital of England?"); 
    if (answer1.toUpperCase() === "LONDON") { 
    alert("That's correct!") 
    correct += 1 
    } 
    else { 
    alert("False. The correct answer is London.") 
    } 

var answer2 = prompt("What is the capital of France?") 
    if (answer1.toUpperCase() === "PARIS") { 
    alert("That's correct!") 
    correct += 1 
    } 
    else { 
    alert("False. The correct answer is Paris."); 
    } 

var answer3 = prompt("What is the capital of Canada?") 
    if (answer3.toUpperCase() === "OTTAWA") { 
    alert("That's correct!"); 
    correct += 1 
    } 
    else { 
    alert("False. The correct answer is Ottawa."); 
    } 

document.write("<h1>You answered " + correct + " out of 5 questions correctly.</h1>") 

if (correct === 5) { 
    document.write("You won a gold star!"); 
} 
    else if (correct >= 3) { 
    document.write("You won a silver star."); 
} 

    else if (correct >= 2) { 
    document.write ("You win third place."); 
} 

else { 
    document.write("Sadly, you did not win any stars."); 
} 

ご覧のとおり、これは非常に長く、ノービスです。ここで私が取り組んできた再書き込みがある:私は少し失われたを取得しています

var questions = [ 
["What is the capital of England?", "LONDON"] 
["What is the capital of France?", "PARIS"] 
["What is the capital of Canada?", "OTTAWA"] 
] 

var correctAnswers = 0; 

for (var i = 0; i < questions.length ; i += 1) { 
question = questions[i][0]; 
answer = questions[i][1]; 
response = prompt(question); 
response = toUpperCase(response); 
if response === answer { 
correctAnswers += 1; 
} 

は警告ウィンドウにクイズでユーザに表示されている正解の構造と配置であります彼らが間違って答えた場合。 3次元配列を2次元配列に追加してforループで参照する必要がありますか?つまり、correctResponse = [i] [2]ですか?それとも私はこれについて別の方法を取っていますか?

+0

私はこれがあなたのためだけの学習段階ですけど、私はあなたが答えたり、クライアント側のJavaScriptで貴重なものを保存することはありません知っている願っています。 –

答えて

1

あなたはいくつかのカンマが欠落していて、配列が乱れています。アレイの構造を複雑にする必要はありません。あなたは個々の配列項目の間にカンマを持っていませんでした。彼らは質問を右に得れば

は基本的に完全な配列を通って、それぞれのあなたのループは、あなたは答えのためのカスタム応答と

var questions = [ 
["What is the capital of England?", "LONDON"], 
["What is the capital of France?", "PARIS"], 
["What is the capital of Canada?", "OTTAWA"] 
]; 

var correctAnswers = 0; 

for (var i = 0; i < questions.length; i++) { 
    var answer = prompt(questions[i][0]); 
    if (answer.toUpperCase() == questions[i][1]) { 
     alert("Correct!"); 
     correctAnswers++; 
    } 
    else { 
     alert("incorrect, the correct answer was " + questions[i][1]); 
    } 
} 

例を質問のquestions[i][0]questions[i][1]をプリントアウトサブアイテム: あなただけの別を追加しますアイテムを各質問配列に追加し、質問が正しい場合にのみそのアイテムを表示します。

var questions = [ 
["What is the capital of England?", "LONDON", "You know, where the queen lives"], 
["What is the capital of France?", "PARIS", "Remember the eiffel tower?"], 
["What is the capital of Canada?", "OTTAWA", "Lot of mooses there"] 
]; 

var correctAnswers = 0; 

for (var i = 0; i < questions.length; i++) { 
    var answer = prompt(questions[i][0]); 
    if (answer.toUpperCase() == questions[i][1]) { 
     alert("Correct! " + questions[i][2]); 
     correctAnswers++; 
    } 
    else { 
     alert("incorrect, the correct answer was " + questions[i][1]); 
    } 
} 
+0

ありがとう!個々の質問ごとにユニークなアラートを追加する方法を知っていますか?すなわち「正解はロンドンです。あなたが知っているのは、クイーンが住んでいる場所」です。 – JSJunkie

+0

はい、確かに、私は数分で例を投稿します。 – Keatinge

+0

@JSJunkieそこでは、彼らがそれを正しく取得すると、各質問のカスタムメッセージを表示する例を投稿しました – Keatinge

関連する問題