lundi 22 mai 2017

Comparing values of 2 dimensional array to single dimensional array

I am creating a trivia game for a class and I am struggling to compare all of the values of a single index of a 2 dimensional array to a single value of a single index of another array. From my limited experience I am using an if statement to compare these values. I will move on to the next question if the condition does work. I must be missing a step but I am unsure how to solve it. The line of code for which I think the mistake lies is $(".choice").on('click', function() {});

Thank you for any help in advanced.

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://ift.tt/2rJhhj4", "http://ift.tt/2qKTNNj", "", "", ""];

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
}

Aucun commentaire:

Enregistrer un commentaire