Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: multiple if else statements

1. ## multiple if else statements

I am trying to set up a booking form for a conference.
The organisers want to offer discount where for every 5 places booked the sixth one is free.

I have tried to write the script that will calculate the total taking this discount into account. It works when I write if(el.value >= 6) total = (el.value * 40) -40; for the first if statement
but when I change it to if(el.value >= 6 and el.value <=11) total = (el.value * 40) -40;
the form stops working.
I am aware I need to add further 'else if' statements for bigger group bookings but I would first like to get this basic one working.

I am sure there is a 'prettier' mathematical formula for doing this calculation which if anyone can helpwith I would much appreciate it!

Here is my complete script so far...

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

var total = 0;
var cf = "totalCost";

var el = document.getElementById('delegates');
if(el.value >= 6 and el.value <=11) total = (el.value * 40) -40; else total = el.value * 40;

document.getElementById('totaloutput').innerHTML = "&pound;" + total;
document.getElementById('totalprice').value = total;

}
</script>

<link href="style.css" rel="stylesheet" type="text/css" />

<body>

<p>&nbsp;</p>
<p>&nbsp;</p>
<form method="post" action="https://www....">

<input type="hidden" name="redirect" value="http://www..../thanks.htm" />
<input type="hidden" name="totalprice" id="totalprice" value="0" />

<table width="60%" border="0" align="center" cellpadding="5" cellspacing="0">

<tr bgcolor="#ECD399">

<td width="672"><span class="style3"><strong>Total Number of conference places </strong></span></td>
<td width="250" colspan="2"><input type="text" name="delegates" id="delegates" value="0" onchange="updateCost();" />

</tr>

<tr bgcolor="#ECD399">

<td><span class="style3">Total cost:</span></td>
<td id="totaloutput">&pound;0.00 </td>

<td id="totaloutput"><input type="button" name="Button" value="Calculate" /></td>
</tr>
<tr bgcolor="#ECD399">
<td colspan="3"><input type="submit" name="Submit" value="Submit" />
&nbsp;&nbsp;
<input type="reset" name="Reset" value="Reset" /></td>
</tr>

</table>

</form>

• if(el.value >= 6 and el.value <=11) total = (el.value * 40) -40;
if(el.value >= 6 && el.value <=11) total = (el.value * 40) -40;

Kinda lazy right now, it might be

• Code:
```if(el.value >= 6 && el.value <=11){
total = el.value * 40 - 40
}else{
total = el.value * 40
}```
This should do. -CC

• Thank you,

That did the trick!

• Can't you simply do:
Code:
`total = 40 * (el.value - Math.floor(el.value / 6));`
dumpfi

• Good alternative method though the first solution allows him to easily change his discount to any value he likes. -CC

• "The organisers want to offer discount where for every 5 places booked the sixth one is free."

So surely Dumpfi's code is correct, allowing for 1, 2, 3, 4, etc. free places if 6, 12, 18, 24 etc. places booked.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•