Klapsius
Klapsius

Reputation: 3359

Simple javascript if statement but can't find where is wrong

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;

Upvotes: 1

Views: 143

Answers (2)

T.J. Crowder
T.J. Crowder

Reputation: 1074168

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).

Upvotes: 5

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

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');
}

Upvotes: 1

Related Questions