javascript - Two if statements are always linked even if it's not true -
i have 2 separate if else statements. if think 1 true, other gets called , vice versa. here is
the first one
if (pension < 0 ) { alert("pension value error. try again."); } else if (pension > income) { alert("rrsp contribution cannot exceed income."); }
the second one
if (uniondues < 0 ) { alert("union dues value error. try again."); } else if (uniondues > (income - pension)) { alert("union dues cannot exceed income less rrsp contribution"); }
the if(pension > income) , if(uniondues > (income - pension)) call each other.
the variable income prompted before hand , checks after check if values valid.
if income 100, , pension 50 , uniondues 60, think should call second if else statement calls both.
if income 1, , pension 2, , uniondues 0, both alerts alerted well. know issue is?
edit: fix simple, parsefloat() , worked.
first off, should make sure 3 of values numbers, not strings because string comparisons not work numbers have different number of digits. want here actual number. if these come users typing data, have convert them numbers using parseint(nnn, 10)
.
then, once numbers, logic has problems.
if pension
greater income
, both else if
statements true.
the first else if
obvious since it's direct else if (pension > income)
, if pension positive, not match first if
.
the second else if (uniondues > (income - pension))
match because income - pension
negative means position value uniondues
match condition.
if want 1 alert ever triggered, can put 4 conditions same logic statement either using 1 if
, 3 else if
or other form of comparison ever selects 1 condition.
another possible solution accumulate error strings and, if error string non-empty @ end, show 1 alert error conditions in it.
perhaps need show first error encoutered (if values true numbers):
if (pension < 0 ) { alert("pension value error. try again."); } else if (pension > income) { alert("rrsp contribution cannot exceed income."); } else if (uniondues < 0 ) { alert("union dues value error. try again."); } else if (uniondues > (income - pension)) { alert("union dues cannot exceed income less rrsp contribution"); }
Comments
Post a Comment