Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tools/ai-review-smoke.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Temporary file for testing the ai-review Feishu notification. This pull request will be closed without merging.
export function average(items) {
let total = 0;
for (let i = 0; i <= items.length; i++) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 高 循环边界越界(off-by-one):条件 i <= items.length 会在最后一轮访问 items[items.length],该值为 undefined,第 5 行读取 .value 时会抛出 TypeError: Cannot read properties of undefined。只要传入数组,函数必然抛错,无法返回结果。

建议改为 i < items.length,或直接使用:

const total = items.reduce((sum, item) => sum + item.value, 0);

total += items[i].value;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 低 缺少输入校验:如果某个元素的 value 缺失或不是数字,total 会变成 NaN,或者因为字符串拼接得到错误结果。如果函数需要正式使用,建议校验 Number.isFinite(items[i].value),或在文档中注明输入约定。

}
return total / items.length;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 中 空数组未处理:修复越界后,items 为空数组时会返回 0 / 0,即 NaN,调用方很难察觉。请明确空输入的约定,例如提前返回 0、null,或抛出带说明的错误:

if (items.length === 0) {
  throw new RangeError('items must not be empty');
}

}
Loading