From 7b91ad7f695a1c3d747ed72a0df302c8ea0f055f Mon Sep 17 00:00:00 2001 From: BinHong Lee Date: Wed, 1 Nov 2017 14:25:18 -0700 Subject: [PATCH] Fixed final person owing itself microcents due to float leftover --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 48af457..1e13d79 100644 --- a/index.js +++ b/index.js @@ -77,8 +77,8 @@ app.post('/emailPaymentChain', function (req, res) { res.json(emails) }) -// app.listen(5000) -app.listen(process.env.PORT) +app.listen(5000) +// app.listen(process.env.PORT) function total (input) { var total = 0 @@ -158,7 +158,7 @@ function paymentChain (users) { function paymentNetwork (users, backup) { var chain = [] - while (chain.length < users.length && backup.length > 0) { + while (chain.length < users.length && backup.length > 1) { if (backup.length > 0 && backup[0].amount === 0) { backup.splice(0, 1) } else if ((0 !== (backup.length - 1)) && (backup[0].amount === (backup[backup.length - 1].amount * -1))) { @@ -176,7 +176,7 @@ function paymentNetwork (users, backup) { } } - if (backup.length > 0) { + if (backup.length > 1) { return paymentChain(users) } else { return chain