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

请帮我修正FreeCodeCamp算牌(CardCounting)代码并指出问题

代码错误分析与修正

你的代码存在两个核心问题:

  • 返回语句错误使用了card变量,正确应该返回当前的count数值,而非传入的牌面参数card;
  • else分支里的card = 0和count = count属于冗余代码,对逻辑毫无作用,完全可以删除。

修正后的代码如下:

let count = 0;

function cc(card) {
  // Only change code below this line
  if ([2,3,4,5,6].includes(card)){
    count++;
  } else if ([10, 'J', 'Q', 'K', 'A'].includes(card)) {
    count--;
  }
  // 牌面为7、8、9时,count保持不变,无需额外处理
  return count > 0 ? `${count} Bet` : `${count} Hold`;
  // Only change code above this line
}

逻辑说明:

  • 当牌面是2-6时,count加1;牌面为10、J/Q/K、A时,count减1;
  • 牌面为7、8、9时,count不做变动,不需要额外分支处理;
  • 根据count的数值返回对应结果:正数返回[数值] Bet,零或负数返回[数值] Hold,使用模板字符串让代码更易读。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 20:50:35