派送3送1配方

Ste*_*ong 0 javascript math

我想得出买3送1的免费公式,例如:

Buy 1, pay price of 1
Buy 2, pay price of 2
Buy 3, pay price of 2
Buy 4, pay price of 3
Buy 5, pay price of 4
Buy 6, pay price of 4
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

1 - 0
2 - 0
3 - 1
4 - 1
5 - 1
6 - 2
7 - 2
8 - 2
9 - 3
Run Code Online (Sandbox Code Playgroud)

依此类推,但我无法将其公式化为n-1等等。

有没有人可以参考我的参考资料?

Ale*_*kov 5

totalPrice = Math.ceil(totalItems / 3 * 2);
Run Code Online (Sandbox Code Playgroud)

每3件商品您要支付2的价格。如果您多买了1-2件商品,则要全额支付。