Skip to content Skip to sidebar Skip to footer

How To Replace Two Eval() In A Comparison

In the following snippet of code: case 'lessthan': { if (eval(firstvalue) >= eval(secondvalue)) { compare = ' should be less than '; compRetu

Solution 1:

If you are expecting int values, you can use parseInt, and you should also check the return value to ensure you are getting a usable number back.

case "lessthan": {
    var a = parseInt(firstvalue, 10), // 10 specifies base, so inputs aren't
        b = parseInt(secondvalue, 10); // interpreted as hex, octal, etc
    if ( isNaN(a) || isNaN(b) ) { 
        // TODO a parse failed, handle your error condition
    }
    else if (a >= b) {
        compare = " should be less than ";
        compReturn = false;
    }
    break;

Post a Comment for "How To Replace Two Eval() In A Comparison"