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


simple if can't find problem. why form return alert if values correct?

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

if values correct or not correct return alert time;

updated answer based on comments , updated question:

based on names "value" , "tolerance" , comments below, think problem in need value - tolerance when doing < 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 instance, suppose value 450 , tolerance 15, , we're testing (test) value 440:

// 440 should fine if want 450 -+ 15 test = 440;  // original test: console.log(440 < (450 + 15)); // true => fails, alert -- 440 < 465  // corrected test: console.log(440 < (450 - 15)); // false, value okay 

i put tests tip1 , tip2 in same order, avoid confusion, , give myself meaningful name value + tolerance , 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 > (value + tolerance)

or

#tip1's value < (value + tolerance)

or

#tip2's value > (value + tolerance)

or

#tip's value > (value + tolerance)

..then alert.

only one of them has true show alert. if you're seeing alert, means 1 of 4 conditions true. can find out one.

those first 2 conditions, of course, can combined into

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

speculations on error is:

  • you didn't mean use value 3 different elements (#tip1, #tip2, #tip), e.g., perhaps @ least 1 of typo (the last pretty suspicious).

  • if both value , tolerance strings (for instance, if got them .val() in code haven't shown), value + tolerance doing string concatenation, not addition. e.g., "1" + "2" "12". happening, both have strings rather numbers (1 + "2" 3, , "1" + 2 3).

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


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -