我需要根据产品重量来计算船价。
<航运价格$5重量价格$6重量11-25H 212F 213
如果我输入的重量介于1到10之间,那么航运价格显示的不正确。如何纠正这些错误?
function sprice() {
let a = 0.5;
let b = 1;
let c = 3;
let d = 5;
let e = 10;
let f = 25;
let g = 1; //0-.5
let h = 2; //.6-1
let i = 3; //2-3
let j = 4; //4-5
let k = 5; //6-10
let l = 6; //11-25
let z = 0;
let inc = 0;
let p = document.getElementById("number").value;
let s = 25;
while (p > 0) {
if (p <= a) {
z += g;
}
if (p <= b) {
z += h;
}
if (p <= c) {
z += i;
}
if (p <= d) {
z += j;
}
if (p <= e) {
z += k;
}
if (p <= f) {
z += l;
}
++inc;
let y = inc * z;
document.getElementById("ans").innerHTML = "$" + y;
p -= s;
}
}<form>
Enter Weight:<input type="text" id="number" name="number" /><br />
<input type="button" value="get" onclick="sprice()" />
</form>
</br>
<p>Shipping Price:<span id="ans"></span></p>
发布于 2021-10-08 09:33:58
您应该能够在这里简化您的逻辑,创建一个函数getShippingPrice()来获取给定重量的发货价格,然后将其添加到总价格中,直到所有权重都计算在内:
function sprice()
{
let weight = Number(document.getElementById("number").value);
let s = 25;
let totalPrice = 0;
while (weight > 0) {
totalPrice += getShippingPrice(weight);
weight -= s;
}
document.getElementById("ans").innerHTML = "$" + totalPrice;
}
function getShippingPrice(weight) {
if (weight <= 0.5 ) return 1;
if (weight <= 1 ) return 2;
if (weight <= 3 ) return 3;
if (weight <= 5 ) return 4;
if (weight <= 10 ) return 5;
return 6;
}<form>
Enter Weight:<input type="text" id="number" name="number"/><br/>
<input type="button" value="get" onclick="sprice()"/>
</form> </br>
<p>Shipping Price:<span id="ans"></span></p>
发布于 2021-10-08 08:22:58
逻辑
检查用户输入是否大于25 input
您遇到的问题是必须使用else if而不是简单的else,因为如果输入一个值,比如0.1,它满足所有的if条件。总金额将是错误的。此外,您也不必将增量索引乘以与y = (inc*z);相同的值。您只需要继续在循环中添加和,每次减少25次,同时执行循环。
工作部件
function sprice() {
let a = 0.5; let b = 1; let c = 3; let d = 5; let e = 10; let f = 25;
let g = 1; //0-.5
let h = 2; //.6-1
let i = 3; //2-3
let j = 4; //4-5
let k = 5; //6-10
let l = 6; //11-25
let z = 0;
let userInput = +document.getElementById("number").value;
let s = 25;
let quantity = 0;
while (userInput > 0) {
quantity = userInput > s ? s : userInput;
if (quantity <= a) {
z += g;
} else if (quantity <= b) {
z += h;
} else if (quantity <= c) {
z += i;
} else if (quantity <= d) {
z += j;
} else if (quantity <= e) {
z += k;
} else if (quantity <= f) {
z += l;
}
userInput -= s;
}
document.getElementById("ans").innerHTML = "$" + z;
}<form>
Enter Weight:<input type="text" id="number" name="number" /><br />
<input type="button" value="get" onclick="sprice()" />
</form> </br>
<p>Shipping Price:<span id="ans"></span></p>
发布于 2021-10-08 09:33:20
这里有一个更加干净、可靠和可伸缩的解决方案。我认为代码很简单,所以我不会解释它。但是如果你什么都不懂,请随便问我:)
// Add more categories if you need
const categories = [{
price: 1,
range: [0, 0.5]
},
{
price: 2,
range: [0.6, 1]
},
{
price: 3,
range: [2, 3]
},
{
price: 4,
range: [4, 5]
},
{
price: 5,
range: [6, 10]
},
{
price: 6,
range: [11, 25]
},
];
const [priceForMaxWeight, maxWeight] = categories.reduce(
([maxPrice, maxWeight], category) => {
const [min, max] = category.range;
if (min < 0 || max < 0 || min === max || min > max)
throw new Error(`Invalid range: [${min}, ${max}]`);
return max > maxWeight ? [category.price, max] : [maxPrice, maxWeight];
}, [Infinity, -Infinity]
);
function getPrice(weight) {
const isInRange = (value, [min, max]) => value >= min && value <= max;
for (let i = 0; i < categories.length; i++) {
const {
price,
range
} = categories[i];
if (isInRange(weight, range)) return price;
}
throw new Error(`Weight: ${weight} doesn't fall into any category.`);
}
function calculatePrice(weight) {
if (weight < 0) throw new Error(`Invalid weight: ${weight}`);
if (weight > maxWeight)
return priceForMaxWeight + calculatePrice(weight - maxWeight);
return getPrice(weight);
}
// Interacting with dom
const calculateButton = document.getElementById("btn");
const result = document.getElementById("result");
const weightInput = document.getElementById("weight");
calculateButton.addEventListener("click", () => {
const weight = Number(weightInput.value);
result.innerText = calculatePrice(weight) + "$";
});<p>Enter Weight:</p>
<input id="weight" type="number" min="0" /><br />
<button id="btn">Calculate Price</button>
<p>Price: <span id="result">0$</span></p>
https://stackoverflow.com/questions/69492503
复制相似问题