I have a function in JS to compare two integers with an arbitrary operator:
function compare(n1, n2, crit){
var operator;
switch (crit) {
case 'GT':
operator = '>';
break;
case 'GE':
operator = '>=';
break;
case 'EQ':
operator = '==';
break;
case 'LE':
operator = '<=';
break;
case 'LT':
operator = '<';
break;
}
return eval(n1 + operator + n2);
}
I'm looking for a more elegant approach than the concatenation and the use of the much dreaded eval()
. Any ideas?