You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改金额增减函数,使其递增上限设置为120?

修改金额调整函数的递增上限

只需修改incrementAmount函数中的上限判断条件,将原来的最大值2替换为120即可,递减函数的逻辑无需改动(下限仍保持1)。

修改后的完整代码如下:

const decrementAmount = () => {
   let newAmount = Amount - 1;
   if (newAmount < 1) {
     newAmount = 1;
   }
   setAmount(newAmount);
 };

 const incrementAmount = () => {
   let newAmount = Amount + 1;
   if (newAmount > 120) {
     newAmount = 120;
   }
   setAmount(newAmount);
 };

如果后续可能需要调整上下限值,建议把常量抽离出来,让代码更易维护:

const MIN_AMOUNT = 1;
const MAX_AMOUNT = 120;

const decrementAmount = () => {
   let newAmount = Amount - 1;
   if (newAmount < MIN_AMOUNT) {
     newAmount = MIN_AMOUNT;
   }
   setAmount(newAmount);
 };

 const incrementAmount = () => {
   let newAmount = Amount + 1;
   if (newAmount > MAX_AMOUNT) {
     newAmount = MAX_AMOUNT;
   }
   setAmount(newAmount);
 };

内容的提问来源于stack exchange,提问作者Van Duo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 01:05:27