0

I have a random math generator and a .get command that sets the value from the returned data. For instance the input #Captcha would get it's value as such:

$.get('PHP/random.php',
    function(data) {
    $("#Captcha").val(data);
});

If the returned data was something like 2 + 5 how would you create a rule that turns the value into two integers, strips out the "+" and then combines the two integers?

1 Answer 1

1

Not sure if this is exactly what you're looking for. But if you just to add the two (or more) numbers together this should work:

var test = "2+5";
var numbers = test.split('+');
var result = 0;
for (var i = 0; i < numbers.length; i++){
    result += parseInt(numbers[i]);
}

Working example - http://jsfiddle.net/L8VMH/

Sign up to request clarification or add additional context in comments.

3 Comments

Since the numbers will be randomly generated, not always 2 + 5 would something like this work: var test = $("#Captcha").val(); ?
Should work fine. I've added a jsfiddle example that does just that.
awesome, really appreciate it.

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.