Forum Moderators: open
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>
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.
<!--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>
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>
// 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]
<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>
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);