javascript - 比较二维数组和一维数组的值

原文 标签 javascript jquery arrays if-statement conditional

我正在为一个类创建琐事游戏,并且我正在努力将二维数组的单个索引的所有值与另一个数组的单个索引的单个值进行比较。根据我有限的经验,我正在使用if语句来比较这些值。我一定错过了一步,但不确定如何解决。我认为错误所在的代码行是$(".choice").on('click', function() {});

感谢您对高级的任何帮助。

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
}

最佳答案

在将问题的答案与正确答案进行比较时,需要包括用户选择的选项的索引。尝试这样的事情:

$("#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");
    }
});

关于javascript - 比较二维数组和一维数组的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44125103/

相关文章:

php - Plupload浏览按钮在IE10中不起作用

javascript - jQuery列表项删除

javascript - 如何将内联JS编码转换为外部样式表?

javascript - 调用以变量命名的jQuery方法

php - 如何创建n级(多级,级联)相关的下拉列表(选择,选项)?

arrays - Swift,字符串检查

mysql - 依靠积极的变化,而依靠消极的变化

javascript - jQuery的多个ID选择器通过PHP的 echo

javascript - 在Highchart JS的气泡图中显示自定义的鼠标悬停

javascript - 根据D3中的属性值对对象进行排序