gpt4 book ai didi

javascript - 如何设置一个根据另一个变量而变化的变量

转载 作者:行者123 更新时间:2023-11-28 19:30:16 26 4
gpt4 key购买 nike

更具体地说,我正在尝试制作一个程序来计算价格。你买的越多,价格应该就会改变。

<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 不断返回零。如何使费率随金额变化。抱歉没有发布全部内容

最佳答案

您在某些地方引用了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 解析为 float 。现在它是一个字符串:

amountPurchased = parseFloat(prompt("Valued customer, please enter the amount of cement you would like to purchase." ));

顺便说一句,对于 15000,您的两个条件将返回 true:

if (amountPurchased <= 15000)
if (amountPurchased >= 15000)

关于javascript - 如何设置一个根据另一个变量而变化的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26962855/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com