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
https://forums.phpfreaks.com/topic/58110-solved-if-within-if/
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
https://forums.phpfreaks.com/topic/58110-solved-if-within-if/#findComment-288344
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
https://forums.phpfreaks.com/topic/58110-solved-if-within-if/#findComment-288663
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.