Forum Moderators: open

Message Too Old, No Replies

Need help with calculator script

         

BigAl

12:16 pm on Oct 1, 2005 (gmt 0)

10+ Year Member



I want to offer a rebate calculator so my customers can calculate thier own rebate. The rebate amount will be based on a percentage of the sale price. For example here is the break down:

enter(price) = $xx.xx

if price > $80 = -50%
if price < $60 = -35%
if price < $40 = -25%

calculate(rebate) = $xx.xx

The problem I'm having with my script is that it doesn't calculate the formula properly. It just displays the percent off not the amount of the rebate. Can anyone help me figure this out?

Here is what I have so far:

<!--Rebate Calculator-->

<script>
<!--
/*
Rebate Calculator
*/

function calculateDiscount(price){

var price = document.NumF.Num.value ;

if(price>80) {
discount=50;

}

else if (price<=60 && price >40) {
discount=35;
}

if(price<=40) {
discount=25;
}
netprice = price * (1-parseFloat(discount/100))
document.getElementById("Morev1").style.display = "block";
document.getElementById("Morev2").innerHTML = netprice;

}

//-->
</script>

<form name="NumF">

<input type="text" value="" name="Num">
<input type="button" value="Caclulate" onClick="calculateDiscount(document.NumF.Num.value)">
<br/>
<span id="MoreV1" style="display:none">The rebate is :</span>
<span id="MoreV2"> </spaN>
</form>

Bernard Marx

1:35 pm on Oct 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Need to know something first:

if price > $80 = -50%
if price < $60 = -35%
if price < $40 = -25%

What's the discount when the price is in range [ $60, $80 ]?

--------
..or do you mean this?:

if price > $80 = -50%
if price > $60 = -35%
if price > $40 = -25%

BigAl

6:41 pm on Oct 1, 2005 (gmt 0)

10+ Year Member



Hmm it looks like you are correct.

I think it should be:

if price > $80 = -50%
if price > or < $60 = -35%
if price > or <$40 = -25%

Bernard Marx

7:06 pm on Oct 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



er.. now it makes no sense at all!

BigAl

7:13 pm on Oct 1, 2005 (gmt 0)

10+ Year Member



LOL, you stumped me. I'm trying to figure this thing out. I think it needs to be..

if price > $80 = -50%
if price = $79-$60 = -35%
if price = < $59-$1 = -25%

Bernard Marx

7:22 pm on Oct 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This should be OK, if the conditions I have assumed are correct.

function calculateDiscount(price)
{
var price = document.NumF.Num.value ;

if (price > 80)
discount = 50;
else if(price > 60)
discount = 35;
else if(price > 40)
discount = 25;

var netprice = price * (1-discount/100);
document.getElementById("Morev1").style.display = "block";
document.getElementById("Morev2").innerHTML = netprice;
}

However, the message says "The rebate is:".
parseFloat isn't needed, since none of the operations is ambiguous (won't hurt though).
You may find is useful to round (up¦down) the discount, since internal rounding errors may give non-integer results that would be expected to be integers.

Bernard Marx

7:31 pm on Oct 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry. Didn't see you last reply.

According to those specs, you should remove the last condition:

else if(price > 40)
discount = 25;
// becomes //
else
discount = 25;

BigAl

8:42 pm on Oct 1, 2005 (gmt 0)

10+ Year Member



Hmm it still doesn't seem to be calculating the percent OFF. Here it what it looks like now.

<!--Rebate Calculator-->

<script>
<!--
/*
Rebate Calculator
*/

function calculateDiscount(price)
{
var price = document.NumF.Num.value ;

if (price > 80)
discount = 50;
else if(price > 60)
discount = 35;
else
discount = 25;

var netprice = price * (1-discount/100);
document.getElementById("Morev1").style.display = "block";
document.getElementById("Morev2").innerHTML = netprice;
}

//-->
</script>

<form name="NumF">

<input type="text" value="" name="Num">
<input type="button" value="Caclulate" onClick="calculateDiscount(document.NumF.Num.value)">
<br/>
<span id="MoreV1" style="display:none">The rebate is :</span>
<span id="MoreV2"> </spaN>
</form>

Bernard Marx

9:20 pm on Oct 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



? The percent off is
discount
.

or?

whoisgregg

9:23 pm on Oct 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All fixed, i think. If you want to calculate the amount of the rebate, you don't subtract the percentage off from 1, you just multiply the total times the discount. So this code now displays both the amount of discount and the new total.

Also, I removed the passing of the price in the "calculate" button's onClick, since the value is declared in the function already.

<script>  
<!--
/*
Rebate Calculator
*/
function calculateDiscount()
{
var price = document.NumF.Num.value ;
if (price > 80)
discount = 50;
else if(price > 60)
discount = 35;
else
discount = 25;
var amountofdiscount = price * (discount/100);
var newprice = price * (1-(discount/100));
document.getElementById("MoreV1").style.visibility = "visible";
document.getElementById("MoreV2").innerHTML = newprice;
document.getElementById("MoreV3").style.visibility = "visible";
document.getElementById("MoreV4").innerHTML = amountofdiscount;
}
//-->
</script>
<form name="NumF">
<input type="text" value="" name="Num">
<input type="button" value="Caclulate" onClick="calculateDiscount();return false;">
<br/>
<p><span id="MoreV3" style="visibility:hidden;">Your discount is:</span> <span id="MoreV4"> </span></p>
<p><span id="MoreV1" style="visibility:hidden;">Your new total is:</span> <span id="MoreV2"></p> </span>
</form>

BigAl

10:13 pm on Oct 1, 2005 (gmt 0)

10+ Year Member



BRILLIANT! It works. Any way to get the script to round off to the nearest 10 cents? Such as 18.75 instead of 18.7425?

Thank you all very much for your help. This forum is a true assest.

Bernard Marx

10:32 pm on Oct 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



// emulator for older browsers
if(!Number.prototype.toFixed) // buggy with zero
Number.prototype.toFixed = function(d){
return ~~this+'.'+String(Math.round(this*100)).slice(-d);
}

// Either arg can be a numerical string.
// Either arg can be negative.
function roundToDec( n, places, toString )
{
var m = Math.pow(10,places);
n = Math.round(n*m)/m;
if(toString && n>=0 && places>=0 )
return n.toFixed(places);
else
return n;
}

alert( roundToDec(12345.6789, 2) );
alert( roundToDec(12365.6789, -2) );
alert( roundToDec(12345.699, 2 ) );
alert( roundToDec(12345.699, 2, true) );

[edited by: Bernard_Marx at 10:52 pm (utc) on Oct. 1, 2005]

BigAl

10:42 pm on Oct 1, 2005 (gmt 0)

10+ Year Member



Sorry about the edits.. Thanks for your help!

BigAl

11:51 pm on Oct 1, 2005 (gmt 0)

10+ Year Member



After inserting that code into the script. It still doesn't drop off the extra digits. if I type in 77.00 it says 50.050000000000004. How can I get it to just say 50.00? It just needs to be rounded up to the nearest dollar. Sorry, i don't understand. Please clairfy.

<script>
// emulator for older browsers
if(!Number.prototype.toFixed) // buggy with zero
Number.prototype.toFixed = function(d){
return ~~this+'.'+String(Math.round(this*100)).slice(-d);
}

// Either arg can be a numerical string.
// Either arg can be negative.
function roundToDec( n, places, toString )
{
var m = Math.pow(10,places);
n = Math.round(n*m)/m;
if(toString && n>=0 && places>=0 )
return n.toFixed(places);
else
return n;
}

<!--
/*
Rebate Calculator
*/
function calculateDiscount()
{
var price = document.NumF.Num.value ;
if (price > 80)
discount = 50;
else if(price > 60)
discount = 35;
else
discount = 25;
var amountofdiscount = price * (discount/100);
var newprice = price * (1-(discount/100));
document.getElementById("MoreV1").style.visibility = "visible";
document.getElementById("MoreV2").innerHTML = newprice;
document.getElementById("MoreV3").style.visibility = "visible";
document.getElementById("MoreV4").innerHTML = amountofdiscount;
}
//-->
</script>

Bernard Marx

12:45 am on Oct 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to actually apply the function to the amounts before outputting them to the page.

Anyway. I decided to simplify the code, and tailor it to your specific needs.
This function is the only "extra" you need (so drop the other stuff)

function toMoney( n )
{
n = Math.round(n*100)/100;
if(n.toFixed)
return n.toFixed(2);
else
return ~~n+'.'+String(Math.round((n?n:1)*100)).slice(-2);
}

Include that function. Then update these two lines in the main

calculateDiscount
:

document.getElementById("MoreV2").innerHTML = toMoney(newprice); 

document.getElementById("MoreV4").innerHTML = toMoney(amountofdiscount); 

BigAl

1:15 am on Oct 2, 2005 (gmt 0)

10+ Year Member



PERFECT! Thanks for your help.. I wish i was that smart :)