0

this javascript code is not working properly(if-else statement).it has output but when i entered term=35, always the computation on term=36 shows.someone can help me to fix this problem?thanks! i'm just newbie in programming.

<script language="javascript" type="text/javascript">
function calc1(form) {

    var principal = form.principal.value;
    var term = form.term.value;
    var paidup = form.paidup.value;
    var unearn = form.unearn.value;
    form.paidup.value = form.principal.value * 0.01;


    if (principal >= 50000, term = 36) {
        form.unearn.value = principal * .27750;
    }

    else if (principal >= 50000, term = 35) {
        form.unearn.value = principal * .27000;
    }

}
</script>
<FORM>
<table>
<tr>
<td>Enter Principal Amount:</td>
<td><INPUT TYPE ="text" NAME="principal" SIZE=15 ></td>
</tr>
<tr>
<td>Enter Number of Term:</td>
<td><INPUT TYPE ="text" NAME="term" SIZE=15 ></td>
</tr>
<tr> <td><INPUT TYPE ="button" VALUE="Calculate" ONCLICK="calc1(this.form)"></td><td></td> </tr>

</tr> <tr> <td>Paid-Up Share Capital:</td> <td><INPUT TYPE="text" NAME="paidup" SIZE=4 ></td> </tr> <tr> <td>Unearned Income:</td> <td><INPUT TYPE="text" NAME="unearn" SIZE=4 ></td> </tr>

</table>
</form>​
dilaraates
  • 135
  • 1
  • 1
  • 16

2 Answers2

0

You've used comma operator in if. This sets term to 36.

if (principal>=50000, term=36)

I'm not sure what you try to achieve, but maybe this works better:

var term = parseInt(form.term.value, 10),
    principal = parseInt(form.principal.value, 10);

if (principal >= 50000 && term == 36) {...} // (if principal is greater than or equal to 5000 and term equals to 36)
Teemu
  • 22,918
  • 7
  • 53
  • 106
0

there are few problems with your all code and I will address them all because you said you are newbie so I hope this will help :)

  1. Reason why you always gets 36 is because in your if statement you have term = 36(one equal) so you set term to 36.

  2. It is imho pointless to use principal>=50000, term=36 in if statement because first expression is ignored, what I think you want to do is

    if(principal>=50000 && term === 36) {

  3. Notice I used === and not == for more info look here : Which equals operator (== vs ===) should be used in JavaScript comparisons?

  4. Don't use inline handlers(inside of html onclick="some function") please read http://www.quirksmode.org/js/introevents.html

Community
  • 1
  • 1
Peter
  • 240
  • 1
  • 4
  • 12