...

View Full Version : js calculate function, that works on discounts



justme2
09-15-2005, 11:21 PM
I need to build a calculator script. It will be a form that has 3 groups of radio buttons, each button will have a dollar value attached to it. And then a calculate button at the bottom. That seems fairly simple, however I am running in to a problem because I have sort of a discount that I want to apply if the user chooses certian items. For example

Group 1
Item 1 - $10.00
Item 2 - $13.00
Item 3 - $15.00

Group 2
Item 1 - $20.00
Item 2 - $23.00
Item 3 - $25.00

Group 3
Item 1 - $30.00
Item 2 - $33.00
Item 3 - $35.00

So let's say someone chooses Item 1 in the first group, Item 1 in the second group and item 1 in the 3rd group. That would all calculate normally - $60.00. But if a user chooses Item 3 in group 1 and item 3 in group 2 and item 3 in group 3 they would get a discounted price, so rather than it being $75.00, it would be reduced to $60.00, but only if they choose those 3 things.

I'm afraid I am not making any sense. :confused: When they make certain combinations with the items, a discount is applicable. So do I have to write out every possible combination with the special price?

If any of this resembles anything you can help with it is much appreciated.

Thanks

glenngv
09-16-2005, 06:09 AM
<html>
<head>
<title>Calculator</title>
<script type="text/javascript">
var disc = [];
disc["i13_i23_i33"] = 60;
//...add as many combination; format: disc["id1_id2_id3"] = discounted price;

function calculate(oFrm){
var total = 0;
var item1 = getSelectedRadio(oFrm.item1);
var item2 = getSelectedRadio(oFrm.item2);
var item3 = getSelectedRadio(oFrm.item3);
if (item1 && item2 && item3){ //all checked
var d = disc[item1.id + "_" +item2.id + "_" + item3.id];
if (typeof d != "undefined"){ //discount exists in the array
total = d;
}
else{ //no discount
total = Number(item1.value) + Number(item2.value) + Number(item3.value);
}
}
else {
alert("Please select one from each item.");
return;
}
alert("$" + total.toDecimals(2));
}

function getSelectedRadio(oRadGrp){
var len=oRadGrp.length;
for (var i=0; i<len; i++){
if (oRadGrp[i].checked) return oRadGrp[i];
}
return null;
}

//from liorean (http://www.codingforums.com/showthread.php?p=178077#post178077)
Number.prototype.toDecimals=function(n){
n=(isNaN(n))?
2:
n;
var
nT=Math.pow(10,n);
function pad(s){
s=s||'.';
return (s.length>n)?
s:
pad(s+'0');
}
return (isNaN(this))?
this:
(new String(
Math.round(this*nT)/nT
)).replace(/(\.\d*)?$/,pad);
}
</script>
</head>
<body>
<form>
<fieldset>
<legend>Group 1</legend>
<input type="radio" name="item1" id="i11" value="10" /><label for="i11">Item 1 - $10.00</label><br />
<input type="radio" name="item1" id="i12" value="13" /><label for="i12">Item 2 - $13.00</label><br />
<input type="radio" name="item1" id="i13" value="15" /><label for="i13">Item 3 - $15.00</label><br />
</fieldset>
<fieldset>
<legend>Group 2</legend>
<input type="radio" name="item2" id="i21" value="20" /><label for="i21">Item 1 - $20.00</label><br />
<input type="radio" name="item2" id="i22" value="23" /><label for="i22">Item 2 - $23.00</label><br />
<input type="radio" name="item2" id="i23" value="25" /><label for="i23">Item 3 - $25.00</label><br />
</fieldset>
<fieldset>
<legend>Group 3</legend>
<input type="radio" name="item3" id="i31" value="30" /><label for="i31">Item 1 - $30.00</label><br />
<input type="radio" name="item3" id="i32" value="33" /><label for="i32">Item 2 - $33.00</label><br />
<input type="radio" name="item3" id="i33" value="35" /><label for="i33">Item 3 - $35.00</label><br />
</fieldset>
<input type="button" value="Calculate" onclick="calculate(this.form)" />
</form>
</body>
</html>

justme2
09-19-2005, 04:28 PM
glenngv,

Thanks so much for the help. I have modified the code to fit the discounts and it works perfectly. I appreciate the help immensely!! :thumbsup:

Thanks\
JustMe2

glenngv
09-20-2005, 03:03 AM
Glad to be of help. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum