Jump to content

[SOLVED] IF within IF


jug

Recommended Posts

Not a brillaint javascript coder but have been trying to do an IF within an IF. Surely its possible and im just hoping its just a simple syntax problem. All this code does is declare variables from html objects, checks whether some of them have values and if they do finds out the highest value of two variables and decalres another varibale with this highest value.

 

Any response would be much appreciated. Thanks in advance

 

Jug

 

  var inamount1 = document.comboform.line1inputbox.value

  var outamount1 = document.comboform.line1display.value

  var inamount2 = document.comboform.line2inputbox.value

  var outamount2 = document.comboform.line2display.value

  var inamount3 = document.comboform.line3inputbox.value

  var outamount3 = document.comboform.line3display.value

  var inamount4 = document.comboform.line4inputbox.value

  var outamount4 = document.comboform.line4display.value

  var inamount5 = document.comboform.line5inputbox.value

  var outamount5 = document.comboform.line5display.value

 

  if (inamount1 != ""){

 

    if (inamount1 > outamount1){var line1take = inamount1}

    if (outamount1 > inamount1){var line1take = outamount1}

  }

 

  if (inamount2 != ""){

 

    if (inamount2 > outamount2){var line2take = inamount2}

    if (outamount2 > inamount2){var line2take = outamount2}

  }

 

  if (inamount3 != ""){

 

    if (inamount3 > outamount3){var line3take = inamount3}

    if (outamount3 > inamount3){var line3take = outamount3}

  }

 

  if (inamount4 != ""){

 

    if (inamount4 > outamount4){var line4take = inamount4}

    if (outamount4 > inamount4){var line4take = outamount4}

  }

 

  if (inamount5 != ""){

 

    if (inamount5 > outamount5){var line5take = inamount5}

    if (outamount5 > inamount5){var line5take = outamount5}

  }

Link to comment
Share on other sites

Works fine for me. Have you tried setting constant test values instead of using form fields? I just did the following test with code copied from your post and it output the expected result:

 

<script type="text/javascript">
var inamount1 = 23;
var outamount1 = 66;
var inamount2 = 12;
var outamount2 = 1;
if (inamount1 != ""){

    if (inamount1 > outamount1){var line1take = inamount1}
    if (outamount1 > inamount1){var line1take = outamount1}
  }

  if (inamount2 != ""){

    if (inamount2 > outamount2){var line2take = inamount2}
    if (outamount2 > inamount2){var line2take = outamount2}
  }

  document.write(line1take+" "+line2take); //output "66 12"
</script>

Link to comment
Share on other sites

Thanks guys the problem has now been solved. Using

 

var line1take = (inamount1 > outamount1) ? inamount1 : outamount1;

 

that emehrkay supplied and i was able to recode the lines so i didnt need an IF within an IF. The new code looked like this

 

  var line1take = (inamount1 > outamount1) ? inamount1 : outamount1;

  if (inamount1 == ""){var line1take = 0}

  line1take = Number(line1take)

 

and nows works perfectly. Thanks again this forum is always great when youre stuck.

 

Jug

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.