1

Simple if but can't find where is problem. Why my form return alert if values are correct?

if ( (($("#tip1").val()) > (value + tolerance)) || 
     (($("#tip1").val()) < (value + tolerance)) || 
     (($("#tip2").val()) < (value + tolerance)) || 
     (($("#tip2").val() ) > (value + tolerance))
   ){
       alert('STOP');
}

If my values are correct or not correct return alert any time;

9
  • 2
    thats a super simple if statement alright Commented Jun 26, 2014 at 8:50
  • Well, at least whenever you use < you should use value - tolerance with a minus, and also check that $("#tip") is correct. Commented Jun 26, 2014 at 8:53
  • did you check your condition. I think whatever value you give it satisfies the condition because you are using || operator which returns true if one of the condition is true Commented Jun 26, 2014 at 8:56
  • What are the values of $("#tip1").val(), $("#tip2").val(), value, and tolerance? Commented Jun 26, 2014 at 8:57
  • 2
    @T.J.Crowder He just confirmed it in a comment in Girish's answer. Commented Jun 26, 2014 at 8:58

2 Answers 2

5

Updated answer based on comments and updated question:

Based on the names "value" and "tolerance" and your comments below, I think the problem is in you need value - tolerance when doing the < checks. E.g.:

var tip1Value = $("#tip1").val();
var tip2Value = $("#tip2").val();
if ( tip1Value > (value + tolerance) || 
     tip1Value < (value - tolerance) ||  // <== - not +
     tip2Value < (value - tolerance) ||  // <== - not +
     tip2Value > (value + tolerance)
   ){
       alert('STOP');
}

So for instance, suppose value is 450 and tolerance is 15, and we're testing (test) the value 440:

// 440 should be fine if we want 450 -+ 15
test = 440;

// Your original test:
console.log(440 < (450 + 15)); // true => fails, we get the alert -- 440 < 465

// Corrected test:
console.log(440 < (450 - 15)); // false, the value is okay

I would also put the tests for tip1 and tip2 in the same order, to avoid confusion, and probably give myself a meaningful name for value + tolerance and value - tolerance:

var tip1Value = $("#tip1").val();
var tip2Value = $("#tip2").val();
var maxValue = value + tolerance;
var minValue = value - tolerance;
if ( tip1Value > maxValue || 
     tip1Value < minValue ||
     tip2Value > maxValue ||
     tip2Value < minValue
   ){
       alert('STOP');
}

Original answer:

Your statement says:

If #tip1's value is > (value + tolerance)

OR

#tip1's value is < (value + tolerance)

OR

#tip2's value is > (value + tolerance)

OR

#tip's value is > (value + tolerance)

..then do the alert.

Only one of them has to be true to show the alert. So if you're seeing the alert, it means one of those four conditions is true. Only you can find out which one.

Those first two conditions, of course, can be combined into

(($("#tip1").val()) != (value + tolerance))

Speculations on what the error is:

  • You didn't mean to use the value from three different elements (#tip1, #tip2, #tip), e.g., perhaps at least one of those is a typo (the last is pretty suspicious).

  • If both value and tolerance are strings (for instance, if you got them from .val() in code you haven't shown), then value + tolerance is doing string concatenation, not addition. E.g., "1" + "2" is "12". But for that to be happening, both have to be strings rather than numbers (1 + "2" is 3, and "1" + 2 is also 3).

  • Based on the names "value" and "tolerance", when doing the < comparisons, you may have meant to use (value - tolerance). E.g., if the goal is to see if 12 is within 3 (tolerance) of 14, you'd want tipvalue < (value - tolerance).

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

7 Comments

Yes but if i enter values exmp 500 and 500 and my value = 500 and tolerance = 15 then should be ok
I need alert if any one condition is true
if my i enter 500 but my value= 450 then 500 < (450 + 15 ) and 500 > (450 - 15)
@user3747585: Yes, it should. And barring information you haven't shown, it will be.
@user3747585: "if my i enter 500 but my value= 450 then 500 < (450 + 15 ) and 500 > (450 - 15)" You have that backward. The tests should be 500 > (450 + 15 ) and 500 < (450 - 15).
|
1

One possibility is that, $("#tip1").val() will return string type. . So you should convert that sting to number by either use parseInt or parseFloat .

var tip1Value = parseFloat($("#tip1").val());
var tipValue = parseFloat($("#tip").val());
var tip2Value = parseFloat($("#tip2").val());;
if (tip1Value > (value + tolerance) || tip1Value < (value + tolerance)) || tip2Value < (value + tolerance)) || tipValue > (value + tolerance)) {
    alert('STOP');
}

4 Comments

this is not "1" > 2 = false
"$("#tip1").val() will return string type. So you can't use < operator with that." Yes you can. If either argument is a number, the other is coerced to a number.
The OP's code is using the value from multiple different elements (#tip1, #tip2, #tip); your code is only using the value from #tip1.
@T.J.Crowder Sorry. My bad, I edited the if condition.

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.