From c3581899b3b8cb849c35f578bd53017d5b390a99 Mon Sep 17 00:00:00 2001 From: Gagan Bhullar Date: Fri, 30 Aug 2024 11:59:32 -0600 Subject: [PATCH] fix(curriculum): swap test order (#55956) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sem Bauke Co-authored-by: Lasse Jørgensen <28780271+lasjorg@users.noreply.github.com> --- .../build-a-cash-register.md | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/build-a-cash-register-project/build-a-cash-register.md b/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/build-a-cash-register-project/build-a-cash-register.md index ffd58941849ece..ac69672b5a3537 100644 --- a/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/build-a-cash-register-project/build-a-cash-register.md +++ b/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/build-a-cash-register-project/build-a-cash-register.md @@ -372,7 +372,7 @@ assert.strictEqual( ); ``` -When `price` is less than the value in the `#cash` element, total cash in drawer `cid` is greater than change due, individual denomination amounts make impossible to return needed change, and the `#purchase-btn` element is clicked, the value in the `#change-due` element should be `"Status: INSUFFICIENT_FUNDS"` +When the `price` is less than the value in the `#cash` element and the total cash in the drawer (`cid`) is insufficient to cover the change due, the purchase should not proceed. When the `#purchase-btn` is clicked under these conditions, the `#change-due` element should display `"Status: INSUFFICIENT_FUNDS"`. ```js const cashInput = document.getElementById('cash'); @@ -387,26 +387,22 @@ price = (randomCash - randomChange) / 100; cashInput.value = `${randomCash / 100}`; let changeLeft = randomChange; -const _expectedChangeDue = []; const _cashInDrawer = []; for (const [denominationName, denomination] of _money) { const maxCountInChange = Math.floor(changeLeft / denomination); - // If denomination can complete required changeLeft, available amount in drawer cannot - // equal the maximum. Otherwise count in drawer can be greater than maximum count in change. - const drawerCount = _randomNumber( - changeLeft % denomination === 0 ? Math.min(15, maxCountInChange - 1) : 15 - ); + // Amount lower than maximum (adjusted to changeLeft) will ensure total in drawer + // will be lower than needed change. + const drawerCount = _randomNumber(Math.max(0, Math.min(15, maxCountInChange - 1))); const amountInDrawer = drawerCount * denomination; _cashInDrawer.push([denominationName, amountInDrawer / 100]); - const changeCount = Math.min(drawerCount, maxCountInChange); - if (denomination <= changeLeft && changeCount > 0) { - changeLeft -= changeCount * denomination; + if (denomination <= changeLeft && drawerCount > 0) { + changeLeft -= amountInDrawer; } } -// Less pennies than changeLeft makes impossible to return change due. -const drawerCount = _randomNumber(Math.min(15, changeLeft - 1)); -_cashInDrawer.push(['PENNY', drawerCount / 100]); +// Less pennies than changeLeft makes sure total cash in drawer is less than change due. +const count = _randomNumber(Math.min(15, changeLeft - 1)); +_cashInDrawer.push(['PENNY', count / 100]); cid = _cashInDrawer.reverse(); @@ -447,7 +443,8 @@ assert.strictEqual( ); ``` -When `price` is less than the value in the `#cash` element, total cash in drawer `cid` is less than the change due, and the `#purchase-btn` element is clicked, the value in the `#change-due` element should be `"Status: INSUFFICIENT_FUNDS"`. + +When `price` is less than the value in the `#cash` element, total cash in drawer `cid` is greater than change due, but the individual denomination amounts make it impossible to return needed change, when the `#purchase-btn` element is clicked, the value in the `#change-due` element should be `"Status: INSUFFICIENT_FUNDS"` ```js const cashInput = document.getElementById('cash'); @@ -462,22 +459,26 @@ price = (randomCash - randomChange) / 100; cashInput.value = `${randomCash / 100}`; let changeLeft = randomChange; +const _expectedChangeDue = []; const _cashInDrawer = []; for (const [denominationName, denomination] of _money) { const maxCountInChange = Math.floor(changeLeft / denomination); - // Amount lower than maximum (adjusted to changeLeft) will ensure total in drawer - // will be lower than needed change. - const drawerCount = _randomNumber(Math.max(0, Math.min(15, maxCountInChange - 1))); + // If denomination can complete required changeLeft, available amount in drawer cannot + // equal the maximum. Otherwise count in drawer can be greater than maximum count in change. + const drawerCount = _randomNumber( + changeLeft % denomination === 0 ? Math.min(15, maxCountInChange - 1) : 15 + ); const amountInDrawer = drawerCount * denomination; _cashInDrawer.push([denominationName, amountInDrawer / 100]); - if (denomination <= changeLeft && drawerCount > 0) { - changeLeft -= amountInDrawer; + const changeCount = Math.min(drawerCount, maxCountInChange); + if (denomination <= changeLeft && changeCount > 0) { + changeLeft -= changeCount * denomination; } } -// Less pennies than changeLeft makes sure total cash in drawer is less than change due. -const count = _randomNumber(Math.min(15, changeLeft - 1)); -_cashInDrawer.push(['PENNY', count / 100]); +// Less pennies than changeLeft makes impossible to return change due. +const drawerCount = _randomNumber(Math.min(15, changeLeft - 1)); +_cashInDrawer.push(['PENNY', drawerCount / 100]); cid = _cashInDrawer.reverse();