From 91c4ca4dceb65a7b92dab21762a1745d677afa0b Mon Sep 17 00:00:00 2001 From: siiky Date: Mon, 3 Feb 2025 14:34:19 +0000 Subject: [PATCH] fix: ensure `coin-change` is valid before comparing with `subset-sum` --- lib/bill-math.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/bill-math.js b/lib/bill-math.js index 8f5daea5..b093fd46 100644 --- a/lib/bill-math.js +++ b/lib/bill-math.js @@ -160,13 +160,13 @@ function makeChange(outCassettes, amount) { const ss_solution = getSolution_old(outCassettes, amount, BILL_LIST_MODES.VALUE_ROUND_ROBIN) const cc_solution = getSolution(outCassettes, amount) - if (!!ss_solution !== !!cc_solution) { - logger.error(new Error(`subset-sum and coin-change don't agree on solvability -- subset-sum:${!!ss_solution} coin-change:${!!cc_solution}`)) + if (!cc.check(cc_solution, amount.toNumber())) { + logger.error(new Error("coin-change provided a bad solution")) return solutionToOriginalUnits(ss_solution, outCassettes) } - if (!cc.check(cc_solution, amount.toNumber())) { - logger.error(new Error("coin-change provided a bad solution")) + if (!!ss_solution !== !!cc_solution) { + logger.error(new Error(`subset-sum and coin-change don't agree on solvability -- subset-sum:${!!ss_solution} coin-change:${!!cc_solution}`)) return solutionToOriginalUnits(ss_solution, outCassettes) }