我正在做一些專案,我面臨一些問題,實際上我希望通過耳光提高價格,就像我允許客戶選擇預訂的客人一樣,例如產品的主要價格是 100,客人的附加價格是 50,客人的限制是 4,當客戶選擇 5 位客人時,主要價格應在客戶選擇 6 位客人后添加到總價中,然后僅添加 50 價格我正在共享一些代碼,但它無法正常作業。
主價 = 100 附加價 = 50
| 客人 | 價錢 |
|---|---|
| 1 | 100 |
| 2 | 150 |
| 3 | 200 |
| 4 | 250 |
| 5 | 350再次加主價(100) |
| 6 | 400 |
| 7 | 450 |
| 8 | 500 |
| 9 | 600再加主價(100) |
| 10 | 650 |
group_size = 4;
increased_group_size = 4;
total_guests = me.guests;
if(total_guests<=group_size){
increased_group_size -= group_size;
if(increased_group_size==0){
increased_group_size = group_size;
}
}
if(total_guests>increased_group_size){
main_price = main_price;
increased_group_size = group_size;
final_price = main_price;
}
else{
final_price1 = total_guests * me.addtional_price;
}
total = final_price1 final_price me.price;
我嘗試了上面的代碼,但無法正常作業。
uj5u.com熱心網友回復:
將額外客人的數量除以團體人數。對于每個組,添加主要價格和附加價格之間的差額。
function calculate_price(total_guests) {
let additional_guests = total_guests - 1;
let group_size = 4;
let additional_groups = Math.floor(additional_guests / group_size);
let main_price = 100;
let additional_price = 50;
let total_price = main_price additional_guests * additional_price additional_groups * (main_price - additional_price);
return total_price;
}
for (let guests = 1; guests <= 10; guests ) {
console.log(`Guests = ${guests} Price = ${calculate_price(guests)}`);
}
uj5u.com熱心網友回復:
本質上,您需要做的是檢查當前數字是否可以被您的最大數字整除。如果是,則添加一個數字,如果不是,則添加另一個數字。
這是一個作業示例。
const guestNum = $(".guests-total");
const total = $(".total");
$(".button").on("click", (e) => {
let numMult = 1;
const num = parseInt(guestNum.text());
let curTotal = parseInt(total.text());
let testNum = num 1;
if (e.target.classList[1] === "min") {
numMult = -1;
testNum = num;
if (num === 0) {
return;
}
}
while (testNum > 0) {
testNum = testNum - 5;
}
if (testNum !== 0) {
curTotal = numMult * 50;
guestNum.text(num numMult * 1);
total.text(curTotal);
return;
}
curTotal = numMult * 100;
guestNum.text(num numMult * 1);
total.text(curTotal);
return;
});
.wrapper {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-size: 5vw;
gap: 2vw;
}
.row-one {
display: flex;
justify-content: center;
align-items: center;
gap: 3vw;
}
.icon-test {
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div class="wrapper">
<div class="row-one">
<div class="button min">-</div>
<div class="guests-total">0</div>
<div class="button add"> </div>
</div>
<div class="row-two">
<div class="money-total">Total: $<span class="total">0</span></div>
</div>
</div>
</body>
</html>
讓我們分解一下。
在這里,我們獲得了客人數量和金額小計的 DOM 元素。
const guestNum = $(".guests-total");
const total = $(".total");
然后我們注冊一個事件偵聽器,用于單擊按鈕以添加或減少客人。
$(".button").on("click", (e) => {
I set a variable called numMult that I will multiply all my sub-total numbers by to either subtract or add money (I'll explain that more later).
let numMult = 1;
Then I get the actual text contained in the DOM elements and convert those to integers so we can add or subtract from them.
const num = parseInt(guestNum.text());
let curTotal = parseInt(total.text());
Finally I make a new variable that will be used to test if the current number is a multiple of 5 or not.
let testNum = num 1;
Here we check if the button that was clicked is the subtract button or the add button, if it's the subtract button then we will be multiplying all our numbers by -1 so that it subtracts from the subtotal instead of adds to it.
If the current number is 0 we return because there obviously can't be negative guests.
if (e.target.classList[1] === "min") {
numMult = -1;
testNum = num;
if (num === 0) {
return;
}
}
Here we subtract from the total number of guests until num is less than or equal to zero.
while (testNum > 0) {
testNum = testNum - 5;
}
If our test number doesn't come out as zero, we know that the number isn't 5 or divisible by 5, so we add or subtract 50.
if (testNum !== 0) {
curTotal = numMult * 50;
guestNum.text(num numMult * 1);
total.text(curTotal);
return;
}
Otherwise, if our test number is 0, we know that the number of guests is either 5 or a multiple of 5, so we add 100.
curTotal = numMult * 100;
guestNum.text(num numMult * 1);
total.text(curTotal);
return;
I hope this helps out.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/454843.html
標籤:javascript jQuery
下一篇:如何從php中的檔案夾下載檔案?
