0

What I am currently doing is comparing user input to an array of answers without page refresh. This works fine.

What I need to achieve is to do the above and also compare the time the input was entered to seconds which is another value in the object. For simplity sake, let's say time is 20 at the moment. how do i do the comparsion

I have pushed answer into an array but i am opened to other methods.

[
    {
        "id": "1",
        "seconds": "20",
        "answer": "John"        
    },
    {
        "id": "2",
        "seconds": "40",
        "answer": "kwai"
    }
]

JS

var score = 0;
var items = [];
$.getJSON('urlpath', function(data) { 
    $.each(data, function(key, val) { 
        items.push(val.answer);             
    });     

    $('#form_submit').click(function() {            
        var qS = $('#form_answer').val();
        $.ajax({
                type:"GET",
                url:"",
                data: qS,
                datatype: 'html'                
        });             

        if ($.inArray(qS, items) > -1 ) {
                //Do something              

        }
        else {
                 //Do nothing 
        }
        return false;
    });

});

1 Answer 1

1

Try this:

var score = 0;
var items = [];
$.getJSON('urlpath', function(data) {
    $.each(data, function(key, val) {
        items.push([val.seconds, val.answer]);
    });

    $('#form_submit').click(function() {
        var answer = $('#form_answer').val();
        var seconds = $('#form_seconds').val();

        var matches = $.grep(items, function(el) {
            return (el[0] == seconds && el[1] == answer);
        });

        if (matches.length > 0) {
            //Lenght > 0, if specified values within an array              
        }
        else {
            //Do nothing 
        }
        return false;
    });
});​
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.