Friday, August 9, 2013

Create Mathematical Validation in Phone

create input, box and operation div in the html file.
write below code in the js file.
var answer;

var method = ['+', '-', '*'];
var operation;
//var operation='1+2+3';

// load new operation on page laod
new_operation();

// function to create new mathematical operation
function new_operation(){
    operation = get_number() + get_method() + get_number()+ get_method() + get_number();
    $('#operation').html(operation);
}

// get random number 
function get_number() {
    return Math.round((Math.random() * (9 - 1) + 1)).toString();
}

function get_method() {
    // alert(method.length);
    return method[Math.round((Math.random() * (method.length - 2) + 1))];
}

// validate user entered value
$('#validate_btn').click(function() {
   // alert(eval(operation));
    if ($('#ans').val() == eval(operation)) {
        alert('you have done rigth job.');
        new_operation();
        $('#ans').val('');
    } else {
        alert('Try again');
    }
});
get online sample on jsfiddle. http://jsfiddle.net/ENzWp/1/

No comments:

Post a Comment