# Thread: Calculator with if/then statement

1. ## Calculator with if/then statement

I am working on a script that calculates the rental rate for a property. I currently have this code and form, which works as I wish it to.
Code:
```<script type="text/javascript">
function roundCents(amount) {
cents = amount * 100;
cents = Math.round(cents);

str = "" + cents;
len = str.length;

return str.substring(0, len - 2) + "." + str.substring(len - 2, len);
}

function update(form) {
var cleaningfee = 50
form.cleaningfee.value = '\$' + roundCents(cleaningfee);

var subtotal1 = (form.nights.value - 0) * (form.unitcost.value - 0);
var subtotal2 = subtotal1 + (cleaningfee - 0);
form.subtotal.value = '\$' + roundCents(subtotal2);

var tax = subtotal2 / 100 * (12 - 0);
form.tax.value = '\$' + roundCents(tax);

var total = subtotal2 + tax;
form.total.value = '\$' + roundCents(total);

var deposit = 200
form.deposit.value = '\$' + roundCents(deposit);

balance = total + (deposit - 0);
form.balance.value = '\$' + roundCents(balance);
}
function clearForm(oForm) {

var elements = oForm.elements;

oForm.reset();

for(i=0; i<elements.length; i++) {

field_type = elements[i].type.toLowerCase();

switch(field_type) {

case "text":
case "hidden":

elements[i].value = "";
break;
case "select-one":
elements[i].selectedIndex = -1;
break;

default:
break;
}
}
}
</script>
<form>
<table id="calc">
<tr><td>Nights: </td><td><input type="text" name="nights" size="8"></td></tr>
<tr><td>Length of Stay: </td><td><select name="unitcost">
<option value="58">3-5 nights</option>
<option value="52">6-28 nights</option>
<option value="50">29-plus nights</option></select></td></tr>
<tr><td>Cleaning Fee: </td><td><input type="text" name="cleaningfee" value="50.00" size="8"></td></tr>
<tr><td>Subtotal: </td><td><input type="text" name="subtotal" size="8"></td></tr>
<tr><td>Tax (12&#37;): </td><td><input type="text" name="tax" size="8"></td></tr>
<tr><td>Total: </td><td><input type="text" name="total" size="8"></td></tr>
<tr><td>Refundable Damage Deposit: </td><td><input type="text" name="deposit" value="200" size="8"></td></tr>
<tr><td>Balance: </td><td><input type="text" name="balance" size="8"></td></tr>
<tr><td> </td><td><input type="button" onClick="update(this.form)" value="Calculate"><br /><input type="button" value="Reset" name="reset_form" onClick="this.form.reset()"></div></td></tr>
</table>
</form>```
Originally, the rates in this calculator differed by season, so the drop down menu listed seasons/dates. However, now that the rates are being adjusted by length of stay, it would be preferable to get rid of the "Length of Stay" part of the form and have a function that evaluated the value in "Nights" as follows:
• Less than 3 nights, an alert message that says "A minimum of 3 nights stay is required"
• 3-5 nights, multiply the nights by 58
• 6-28 nights, multiply by 52
• 329 or more nights, multiply by 50

The minimum stay message would be ideal, but if it's too complicated, I'd rather lose that and gain this other functionality. However, I just haven't been able to wrap my head around how to write this if/else statement to do the job.

Any pointers?

Cheers!

2. Code JavaScript:
```var numnights = 10 * 1; // Easy way to force the value to numeric
var result = 0;
if (numnights < 3) {
alert ("A minimum of 3 nights stay is required");
} else {
if (numnights >= 3 && numnights <= 5) {
result = numnights * 58;
} else if (numnights >= 6 && numnights <= 28) {
result = numnights * 52;
} else {
result = numnights * 50;
}
}```
(I presume 329 should be 29)

3. Thanks, Ian; and your assumption was correct.
I've added the code you provided below, but the math isn't working out, possibly in part due to the way the value is forced to numeric, nor is the minimum stay alert working. Any hints as to what I've done wrong?
Code:
```<script type="text/javascript">
function roundCents(amount) {
cents = amount * 100;
cents = Math.round(cents);

str = "" + cents;
len = str.length;

return str.substring(0, len - 2) + "." + str.substring(len - 2, len);
}

function update(form) {
var cleaningfee = 50
form.cleaningfee.value = '\$' + roundCents(cleaningfee);

var numnights = 10 * 1; // Easy way to force the value to numeric
var result = 0;
if (numnights < 3) {
alert ("A minimum of 3 nights stay is required");
} else {
if (numnights >= 3 && numnights <= 5) {
result = numnights * 58;
} else if (numnights >= 6 && numnights <= 28) {
result = numnights * 52;
} else {
result = numnights * 50;
}
}
var subtotal1 = result;
var subtotal2 = subtotal1 + (cleaningfee - 0);
form.subtotal.value = '\$' + roundCents(subtotal2);

var tax = subtotal2 / 100 * (12 - 0);
form.tax.value = '\$' + roundCents(tax);

var total = subtotal2 + tax;
form.total.value = '\$' + roundCents(total);

var deposit = 200
form.deposit.value = '\$' + roundCents(deposit);

balance = total + (deposit - 0);
form.balance.value = '\$' + roundCents(balance);
}
function clearForm(oForm) {

var elements = oForm.elements;

oForm.reset();

for(i=0; i<elements.length; i++) {

field_type = elements[i].type.toLowerCase();

switch(field_type) {

case "text":
case "hidden":

elements[i].value = "";
break;
case "select-one":
elements[i].selectedIndex = -1;
break;

default:
break;
}
}
}
</script>```

4. Try changing the first part of the update function to:

Code:
```function update(form) {
var cleaningfee = 50
form.cleaningfee.value = '\$' + roundCents(cleaningfee);

var numnights = parseInt(form.nights.value, 10);

if (numnights < 3) {
alert ("A minimum of 3 nights stay is required");
form.nights.value = 3;
numnights = 3;
}

var result;
if (numnights >= 3 && numnights <= 5) {
result = numnights * 58;
} else if (numnights >= 6 && numnights <= 28) {
result = numnights * 52;
} else {
result = numnights * 50;
}

var subtotal1 = result;```
I am not sure the 'Length of Stay' select element is needed, the way the form is at present.

If you wanted to grab the relevant value out of that element, it can't be done with 'form.unitcost.value', as the original code had. Instead it would need something like this:

Code:
```var nightlycost = form.unitcost.options[form.unitcost.selectedIndex].value;
var subtotal1 = numnights * nightlycost;```
But that could be mis-used here, e.g. someone could enter the minimum 3 nights into the 'Nights' input, then grab themselves the most discounted rate by setting the '29-plus nights' select option. In short, the two elements would need to be synchronised, or else just don't use that select element and do it all as above, in the behind-the-scenes code.

#### Posting Permissions

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