2017-05-23 21 views
1

私はクラスのためのトリビアゲームを作成していますが、2次元配列の1つのインデックスのすべての値を1つのインデックスの1つの値と比較するのは苦労しています別の配列の私の限られた経験から、私はこれらの値を比較するif文を使用しています。私はステップを逃しているに違いないが、それを解決する方法がわからない。間違いがあると思うコード行は、$(".choice").on('click', function() {});2次元配列の値を1次元配列と比較する

ありがとうございました。

JS:

window.onload = function() { 
    $('#start').html('<div class="text-center"><button type="button" class="btn btn-default">Start</button></div>'); 
}; 

var questionArray = ["This bands second album went platinum 5 times in the UK and double Platinum in the US.", "This band was formed in Australia and their first album, which had you Walking On A Dream, has sold over 3 million copies."]; 
var optionArray = [["Radio Head", "Gorillaz", "Coldplay", "Arctic Monkeys"], ["Empire Of The Sun", "M83", "MGMT", "Two Door Cinema Club"]]; 
var answerArray= ["Gorillaz", "Empire Of The Sun"]; 
var imageArray= ["http://cdn3.pitchfork.com/artists/1767/m.65d9c64d.jpg", "http://crowningmusic.com/storage/rA7GUFFoBCtT8Jg4L1tv.png", "", "", ""]; 

var count = 0; 
var question = 0; 

$("#start").on('click', function() { 

    $(this).css("display","none"); 

    timer(
     30000, 
     function(timeleft) { 
      $('#timer').html(timeleft); 
     }, 
     function() { 
      // What happens after // 
     } 
    ); 

    $("#question").html(questionArray[question]); 
     for (var j = 0; j < 4; j++) { 
      $("#options").append('<button class="choice">' + optionArray[question][j] + "</button>" + "<br>"); 
     } 

    $(".choice").on('click', function() { 
     console.log('click'); 
     console.log(answerArray[question]) 
     if (optionArray[question] == answerArray[question]) { 
      console.log("Working"); 
     } 
    }); 
    // $("#holder").html("<img src=" + questionArray[count] + ">"); 
}); 

function nextQuestion() { 
    count++; 
} 

// Timer Function // 
function timer(time,update,complete) { 
    var start = new Date().getTime(); 
    var interval = setInterval(function() { 
     var now = time-(new Date().getTime()-start); 
     if(now <= 0) { 
      clearInterval(interval); 
      complete(); 
     } 
     else update(Math.floor(now/1000)); 
    },100); // the smaller this number, the more accurate the timer will be 
} 
+1

'if(optionArray [question] == answerArray [question])'の期待される結果は何ですか? – guest271314

+0

配列(optionArray [question]にある)が文字列(answerArray [question]にあります)と等しい場合、その行が実際に比較しているようです。これは常に偽です(両面が何らかの理由でnullまたは未定義と評価されない限り)。選択した選択肢のインデックスがありません。あなたは、クリックされた選択肢のインデックスを取得し、次のように使用する必要があります:if(optionArray [choiceIndex] === answerArray [question]) – pacifier21

+0

@pacifier21 $(this).attr()を使って選択した選択肢にattrを追加しますか?そして何とか選択された選択肢の文字列を取り出し、その2つの値を比較します。 – willking

答えて

1

あなたは正しい答えを、質問への回答を比較している場合、ユーザー選択された選択肢のインデックスを含める必要があります。このような何か試してみてください:

$("#question").html(questionArray[question]); 
for (var j = 0; j < 4; j++) { 
    // Include an ID in the choice button 
    $("#options").append('<button class="choice" id="choice_' + j + '">' + optionArray[question][j] + "</button>" + "<br>"); 
} 

$(".choice").on('click', function() { 
    console.log('click'); 
    console.log(answerArray[question]); 

    // Get the index of the selected answer through the ID attribute 
    var selectedAnswerIndex = $(this).attr('id').substring("choice_".length); 
    if (optionArray[question][selectedAnswerIndex] === answerArray[question]) { 
     console.log("Working"); 
    } 
}); 
+0

"条件がうまくいけば次の質問に移ります"。それが正しい場合にのみ、なぜ次の質問に移動するのですか?私は混乱しているthats。私のバグ – karthick

+0

@ pacifier21これはまさに私がやろうとしていたものです。 attrを追加するともっと明らかになりました – willking

0

を回答インデックスを扱っていないもう一つのオプションは、次のようになります。

$(".choice").on('click', function() { 
    console.log('click'); 
    console.log(answerArray[question]) 

    // Use the text content of the button to check the answer 
    if ($(this).text() === answerArray[question]) { 
     console.log("Working"); 
    } 
}); 

注:これは、ボタンが唯一可能な答え値が含まれているという事実に依存タグの間にボタンの中に何かを置くと、この解決法は機能しません。

+0

次の質問と質問のオプションが正しいか間違った答えをクリックした後にdivのhtmlを入れ替えるとソリューションが動作するのですか?私は昨夜あなたの他のソリューションと一緒に働いて、新しい値を配列に追加するだけで、質問と回答が正しく機能するようになりました。それは素晴らしい解決策でした。それ以降は終了する必要があります。 – willking

+0

それぞれの質問が表示された後にクリックイベントコールバックを再接続する必要があると思います。あなたはその質問と回答のすべてを関数に組み込むことができ、次の質問の準備が整ったらその関数を呼び出すことができます。もちろん、次の質問と答えのhtml要素を作成する前に質問インデックス(質問++)をインクリメントする必要があります。私はすべてが理にかなったことを願う。 – pacifier21