跳至內容
Astera Technology

總數明細也要沿原本條件

明細返回真正支持所顯總數的行。

下載完整個案資料及參考答案 ↓

訂判斷及所需輸入

本地報表只收未排除的 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}]}
下載可編輯 CSV ↓

跑所附比較

程序真正跑總數及本地明細函數。返回編號是明細結果約定,未表示真客戶介面已開那些紀錄。

錯誤及所需輸出
路徑輸出 JSON
錯誤路徑{"total":5,"openedIds":["A","B","C","D","E"]}
所需路徑{"total":5,"openedIds":["A","B"]}
下載可編輯 CSV ↓
完整本地 Node24.14.1 程序
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 即使值零,仍屬零總數明細;地區欠缺就停,不打開全體代替。

  1. 核總數條件
  2. 明細沿同一條件
  3. 對總數及返回編號

總額相同未證明逐行來源正確。

可編輯邊界輸入及結果
條件輸入 JSON預期輸出 JSON
合資格行的值真為零{"segment":"North","records":[{"id":"A","segment":"North","excluded":false,"units":0}]}{"total":0,"openedIds":["A"]}
地區未訂{"segment":null,"records":[]}{"held":true}
下載可編輯 CSV ↓

原例總數五,哪些編號支持它?

查看參考答案

只有 A、B。C 已排除,D、E 不屬 North;加入就改了明細意思。

相關資源