更具体地说,我正在尝试制作一个计算价格的程序。价格应该随你购买的越多而改变。
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Transaction Invoice</title>
<meta name="author" content="Caprica" />
<!-- Date: 2014-11-12 -->
</head>
<body>
<script type="text/javascript">
// Program name: Transaction Invoice
// Purpose: To do a transaction
// Author: Ephraim Vickers
// Date last modified: Today
// Variables
var BREAK = "<br />"
var pricePound = 1.13; // This is the price per pound
var amountPurchased = 0; // Number of pounds purchased
var reducRate = parseInt (document.getElementById ("0.02").value); // This is the percent it is reduced
var noDiscount = pricePound * amountPurchased; //Cost without discount
var reducCost = noDiscount - amountSaved;
var amountSaved = noDiscount * reducRate;
document.write ("Hello and thank you for shopping at the ACME cement company. This program is here to handle transaction information and apply discounts. For all other concerns please go to our main site." + BREAK)
window.alert("Please be aware that all cement purchases are on sell and, the more you buy the more it will be discounted!!! All numbers inputted will be converted to pounds. Some restrictions and limitations do apply see a instore clerk for details.")
amountPurchased = prompt ("Valued customer, please enter the amount of cement you would like to purchase." );
noDiscount = pricePound * amountPurchased
reducCost = noDiscount - amountSaved
amountSaved = noDiscount * reducRate
parseFloat(reducRate)
if (amountPurchased <= 0) {
window.alert("ERROR. Your purchase is below our minimum purchase amount. Please refresh the page and increase the amount you are trying to purchase." );
}
if (amountPurchased <= 500) {
reduceRate = 0.02;
}
if (amountPurchased <=9000) {
reduceRate = 0.04;
}
if (amountPurchased <= 15000) {
reduceRate = 0.05;
}
if (amountPurchased >= 15000) {
reduceRate = 0.09;
};
document.write (amountSaved); // This also returns 0 but i think its because of the reducRate
document.write (reducRate); //This is the part that always returns 0
</script>
由于某种原因,reduceRate保持返回零。如何使用金额更改费率。 很抱歉没有发布整件事
答案 0 :(得分:2)
您在某些地方引用reducRate
,在其他地方引用reduceRate
。将reducRate
的所有引用更改为reduceRate
。
另外,您的if语句不能获得费率。您应该使用else if
条件来解决此问题:
function getRate(amountPurchased) {
if (amountPurchased <= 500) {
return 0.02;
}
else if (amountPurchased <=9000) {
return 0.04;
}
else if (amountPurchased <= 15000) {
return 0.05;
}
else { // amountPurchased > 15000
return 0.09;
}
}
console.log(getRate(0) == 0.02);
console.log(getRate(500) == 0.02);
console.log(getRate(501) == 0.04);
console.log(getRate(9000) == 0.04);
console.log(getRate(9001) == 0.05);
console.log(getRate(15000) == 0.05);
console.log(getRate(15001) == 0.09);
你应该将amountPurchased
解析为浮点数。现在它是一个字符串:
amountPurchased = parseFloat(prompt("Valued customer, please enter the amount of cement you would like to purchase." ));
顺便说一下,你的两个条件将在15000时返回true:
if (amountPurchased <= 15000)
if (amountPurchased >= 15000)
答案 1 :(得分:1)
问题很简单:你拼写错误的变量名称。只需将reducRate
更改为reduceRate
(请注意第二个e以reduc e 评分),一切都应该正常工作......
除此之外,您还应该使用else if
代替if
。
所以你的代码看起来像这样:
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Transaction Invoice</title>
<meta name="author" content="Caprica" />
<!-- Date: 2014-11-12 -->
</head>
<body>
<script type="text/javascript">
// Program name: Transaction Invoice
// Purpose: To do a transaction
// Author: Ephraim Vickers
// Date last modified: Today
// Variables
var BREAK = "<br />"
var pricePound = 1.13; // This is the price per pound
var amountPurchased = 0; // Number of pounds purchased
var reduceRate = parseInt (document.getElementById ("0.02").value); // This is the percent it is reduced
var noDiscount = pricePound * amountPurchased; //Cost without discount
var reducCost = noDiscount - amountSaved;
var amountSaved = noDiscount * reduceRate;
document.write ("Hello and thank you for shopping at the ACME cement company. This program is here to handle transaction information and apply discounts. For all other concerns please go to our main site." + BREAK)
window.alert("Please be aware that all cement purchases are on sell and, the more you buy the more it will be discounted!!! All numbers inputted will be converted to pounds. Some restrictions and limitations do apply see a instore clerk for details.")
amountPurchased = parseFloat(prompt ("Valued customer, please enter the amount of cement you would like to purchase." ));
noDiscount = pricePound * amountPurchased;
reducCost = noDiscount - amountSaved;
amountSaved = noDiscount * reduceRate;
if (amountPurchased <= 0) {
window.alert("ERROR. Your purchase is below our minimum purchase amount. Please refresh the page and increase the amount you are trying to purchase." );
} else if (amountPurchased <= 500) {
reduceRate = 0.02;
} else if (amountPurchased <= 9000) {
reduceRate = 0.04;
} else if (amountPurchased <= 15000) {
reduceRate = 0.05;
} else { //only occurs when none of the others are true: so this is when it's >15000
reduceRate = 0.09;
}
document.write (amountSaved); // This also returns 0 but i think its because of the reducRate
document.write (reduceRate); //This is the part that always returns 0
</script>