私はクラスのためのトリビアゲームを作成していますが、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
}
'if(optionArray [question] == answerArray [question])'の期待される結果は何ですか? – guest271314
配列(optionArray [question]にある)が文字列(answerArray [question]にあります)と等しい場合、その行が実際に比較しているようです。これは常に偽です(両面が何らかの理由でnullまたは未定義と評価されない限り)。選択した選択肢のインデックスがありません。あなたは、クリックされた選択肢のインデックスを取得し、次のように使用する必要があります:if(optionArray [choiceIndex] === answerArray [question]) – pacifier21
@pacifier21 $(this).attr()を使って選択した選択肢にattrを追加しますか?そして何とか選択された選択肢の文字列を取り出し、その2つの値を比較します。 – willking