Skip to content

Commit c358189

Browse files
gagan-bhullar-techSembaukelasjorg
authored
fix(curriculum): swap test order (freeCodeCamp#55956)
Co-authored-by: Sem Bauke <[email protected]> Co-authored-by: Lasse Jørgensen <[email protected]>
1 parent 370061a commit c358189

File tree

1 file changed

+23
-22
lines changed
  • curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/build-a-cash-register-project

1 file changed

+23
-22
lines changed

curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/build-a-cash-register-project/build-a-cash-register.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ assert.strictEqual(
372372
);
373373
```
374374
375-
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"`
375+
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"`.
376376
377377
```js
378378
const cashInput = document.getElementById('cash');
@@ -387,26 +387,22 @@ price = (randomCash - randomChange) / 100;
387387
cashInput.value = `${randomCash / 100}`;
388388

389389
let changeLeft = randomChange;
390-
const _expectedChangeDue = [];
391390
const _cashInDrawer = [];
392391
for (const [denominationName, denomination] of _money) {
393392
const maxCountInChange = Math.floor(changeLeft / denomination);
394-
// If denomination can complete required changeLeft, available amount in drawer cannot
395-
// equal the maximum. Otherwise count in drawer can be greater than maximum count in change.
396-
const drawerCount = _randomNumber(
397-
changeLeft % denomination === 0 ? Math.min(15, maxCountInChange - 1) : 15
398-
);
393+
// Amount lower than maximum (adjusted to changeLeft) will ensure total in drawer
394+
// will be lower than needed change.
395+
const drawerCount = _randomNumber(Math.max(0, Math.min(15, maxCountInChange - 1)));
399396
const amountInDrawer = drawerCount * denomination;
400397
_cashInDrawer.push([denominationName, amountInDrawer / 100]);
401-
const changeCount = Math.min(drawerCount, maxCountInChange);
402-
if (denomination <= changeLeft && changeCount > 0) {
403-
changeLeft -= changeCount * denomination;
398+
if (denomination <= changeLeft && drawerCount > 0) {
399+
changeLeft -= amountInDrawer;
404400
}
405401
}
406402

407-
// Less pennies than changeLeft makes impossible to return change due.
408-
const drawerCount = _randomNumber(Math.min(15, changeLeft - 1));
409-
_cashInDrawer.push(['PENNY', drawerCount / 100]);
403+
// Less pennies than changeLeft makes sure total cash in drawer is less than change due.
404+
const count = _randomNumber(Math.min(15, changeLeft - 1));
405+
_cashInDrawer.push(['PENNY', count / 100]);
410406

411407
cid = _cashInDrawer.reverse();
412408

@@ -447,7 +443,8 @@ assert.strictEqual(
447443
);
448444
```
449445
450-
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"`.
446+
447+
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"`
451448
452449
```js
453450
const cashInput = document.getElementById('cash');
@@ -462,22 +459,26 @@ price = (randomCash - randomChange) / 100;
462459
cashInput.value = `${randomCash / 100}`;
463460

464461
let changeLeft = randomChange;
462+
const _expectedChangeDue = [];
465463
const _cashInDrawer = [];
466464
for (const [denominationName, denomination] of _money) {
467465
const maxCountInChange = Math.floor(changeLeft / denomination);
468-
// Amount lower than maximum (adjusted to changeLeft) will ensure total in drawer
469-
// will be lower than needed change.
470-
const drawerCount = _randomNumber(Math.max(0, Math.min(15, maxCountInChange - 1)));
466+
// If denomination can complete required changeLeft, available amount in drawer cannot
467+
// equal the maximum. Otherwise count in drawer can be greater than maximum count in change.
468+
const drawerCount = _randomNumber(
469+
changeLeft % denomination === 0 ? Math.min(15, maxCountInChange - 1) : 15
470+
);
471471
const amountInDrawer = drawerCount * denomination;
472472
_cashInDrawer.push([denominationName, amountInDrawer / 100]);
473-
if (denomination <= changeLeft && drawerCount > 0) {
474-
changeLeft -= amountInDrawer;
473+
const changeCount = Math.min(drawerCount, maxCountInChange);
474+
if (denomination <= changeLeft && changeCount > 0) {
475+
changeLeft -= changeCount * denomination;
475476
}
476477
}
477478

478-
// Less pennies than changeLeft makes sure total cash in drawer is less than change due.
479-
const count = _randomNumber(Math.min(15, changeLeft - 1));
480-
_cashInDrawer.push(['PENNY', count / 100]);
479+
// Less pennies than changeLeft makes impossible to return change due.
480+
const drawerCount = _randomNumber(Math.min(15, changeLeft - 1));
481+
_cashInDrawer.push(['PENNY', drawerCount / 100]);
481482

482483
cid = _cashInDrawer.reverse();
483484

0 commit comments

Comments
 (0)