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

JavaScript函数gatherFeedback返回结果错误排查求助

问题分析与修复

你的代码逻辑错误出在中评的条件范围上,导致4分的评分被错误归类为差评。

错误原因

题目明确中评是4-6分,但你写的中评判断条件是:

else if (feedbackArray[i][1] <= 6 && feedbackArray[i][1] > 4)

这个条件只包含了5、6分,4分被排除在外,会直接进入else分支被算作差评。这就是第三个测试用例里,你的neutral少1个、negative多1个的原因。

修复后的代码

调整中评条件以包含4分,同时可以把评分存为变量让代码更易读:

function gatherFeedback (feedbackArray) {
    let positive = 0;
    let negative = 0;
    let neutral = 0;
    
    for (let i = 0; i < feedbackArray.length; i++) {
        const score = feedbackArray[i][1];
        if (score >= 7 && score <= 10) { // 好评:7-10分
            positive++;
        } else if (score >= 4 && score <= 6) { // 中评:4-6分
            neutral++;
        } else { // 差评:0-3分
            negative++;
        }
    }
      
    return { positive, negative, neutral };
}

逻辑验证

修复后,4分的评分会被正确归到中评类别,对应第三个测试用例的预期输出:{ "positive": 2, "negative": 1, "neutral": 2 },完全符合题目要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 11:10:23