訂判斷及所需輸入
本地報表只收未排除的 North。A2、B3 得總五;錯明細函數卻返回五個來源編號,連已排除 C 及 South 的 D、E 也有。更正返回支持編號時,同樣保地區及排除條件。
| 情況 | 輸入 JSON |
|---|---|
| 原例測試 | {"segment":"North","records":[{"id":"A","segment":"North","excluded":false,"units":2},{"id":"B","segment":"North","excluded":false,"units":3},{"id":"C","segment":"North","excluded":true,"units":7},{"id":"D","segment":"South","excluded":false,"units":8},{"id":"E","segment":"South","excluded":true,"units":9}]} |
跑所附比較
程序真正跑總數及本地明細函數。返回編號是明細結果約定,未表示真客戶介面已開那些紀錄。
| 路徑 | 輸出 JSON |
|---|---|
| 錯誤路徑 | {"total":5,"openedIds":["A","B","C","D","E"]} |
| 所需路徑 | {"total":5,"openedIds":["A","B"]} |
import assert from 'node:assert/strict';
const run=function run(i,corrected=true){
if(i.segment===null)return {held:true};const eligible=i.records.filter(r=>r.segment===i.segment&&!r.excluded);
return {total:eligible.reduce((s,r)=>s+r.units,0),openedIds:(corrected?eligible:i.records).map(r=>r.id)};
};
const cases=[{"input":{"segment":"North","records":[{"id":"A","segment":"North","excluded":false,"units":2},{"id":"B","segment":"North","excluded":false,"units":3},{"id":"C","segment":"North","excluded":true,"units":7},{"id":"D","segment":"South","excluded":false,"units":8},{"id":"E","segment":"South","excluded":true,"units":9}]},"corrected":false,"expected":{"total":5,"openedIds":["A","B","C","D","E"]}},{"input":{"segment":"North","records":[{"id":"A","segment":"North","excluded":false,"units":2},{"id":"B","segment":"North","excluded":false,"units":3},{"id":"C","segment":"North","excluded":true,"units":7},{"id":"D","segment":"South","excluded":false,"units":8},{"id":"E","segment":"South","excluded":true,"units":9}]},"corrected":true,"expected":{"total":5,"openedIds":["A","B"]}},{"input":{"segment":"North","records":[{"id":"A","segment":"North","excluded":false,"units":0}]},"corrected":true,"expected":{"total":0,"openedIds":["A"]}},{"input":{"segment":null,"records":[]},"corrected":true,"expected":{"held":true}}];
for(const test of cases){const actual=await run(test.input,test.corrected);assert.deepEqual(actual,test.expected);console.log(actual);}
按邊界依據作判斷
合資格 A 即使值零,仍屬零總數明細;地區欠缺就停,不打開全體代替。
- 核總數條件
- 明細沿同一條件
- 對總數及返回編號
總額相同未證明逐行來源正確。
| 條件 | 輸入 JSON | 預期輸出 JSON |
|---|---|---|
| 合資格行的值真為零 | {"segment":"North","records":[{"id":"A","segment":"North","excluded":false,"units":0}]} | {"total":0,"openedIds":["A"]} |
| 地區未訂 | {"segment":null,"records":[]} | {"held":true} |
原例總數五,哪些編號支持它?
查看參考答案
只有 A、B。C 已排除,D、E 不屬 North;加入就改了明細意思。