3 回答

TA貢獻(xiàn)1883條經(jīng)驗(yàn) 獲得超3個(gè)贊
重要變化:修改了三元運(yùn)算并更改了循環(huán)測(cè)試
請(qǐng)參閱下面的工作代碼:
let available = 3700, percent = 20
for (let index = 0; index < 100/percent; index++) {
let use = index == 0 ? (percent / 100) * available : available / (100/percent - index)
console.log(`Index: ${index} | Use > ${use}`)
console.log(`Before reduction > ${available}`)
available -= use
console.log(`After reduction > ${available}\n`)
}

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊
在定義使用時(shí)去掉條件。這應(yīng)該解決它。
let available = 3700, percent = 10
let availableBeginning = available
for (let index = 0; available > 0 ; index++) {
let use = percent / 100) * availableBeginning
console.log(`Index: ${index} | Use > ${use}`)
console.log(`Before reduction > ${available}`)
available -= use
console.log(`After reduction > ${available}\n`)
}

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊
這將適用于您的情況。我不確定為什么你有 `available / ((percent - index) % percent)),這基本上只是將你的原始數(shù)字除以你想要扣除的百分比,然后取模原始百分比。因此,在這種情況下,在 0 之后,您的行為是 use = 878.75,因?yàn)槟鷮?3515 除以 (5 - 1) % 5,即 = 4. 3515 / 4 = 878.85。這將扣除得相當(dāng)快,因?yàn)槟看蔚辽賹?1 / 百分比值作為已扣除數(shù)字的整數(shù)。
事實(shí)證明,你的使用邏輯實(shí)際上是沒有意義的,如果你想每次迭代都扣除偶數(shù),你不必根據(jù)任何邏輯設(shè)置它......只需重復(fù)計(jì)算和扣除多少次你想要的.
無論如何,這是解決方案:
let available = 3700, percent = 5;
// This is going to be the constant deduction you will continue to use
let deduction = 3700 * (percent / 100);
for (let index = 0; index < 10; index++) {
console.log(`Index: ${index} | Use > ${deduction}`)
console.log(`Before deduction > ${available}`)
available -= deduction
console.log(`After deduction > ${available}\n`)
}
添加回答
舉報(bào)