diff --git a/lib/vending-machine.js b/lib/vending-machine.js index 4879b4b..7af9517 100644 --- a/lib/vending-machine.js +++ b/lib/vending-machine.js @@ -63,7 +63,7 @@ function getCoinsAvail () { */ function sellProduct (totalPayable, coinsPaid, coinsAvail) { const cashPaid = coinsPaid.reduce( function(sum, c) { return sum + c }, 0); - const change = calculateChange(totalPayable, cashPaid); + const change = calculateChange(totalPayable, cashPaid, coinsAvail); // remove the coins we are about to return as change from the coinsAvail: setCoinsAvail(reduceCoinSupply(coinsAvail, change)); return change; diff --git a/tap-advanced-testing.md b/tap-advanced-testing.md index e415c0e..30b9b5f 100644 --- a/tap-advanced-testing.md +++ b/tap-advanced-testing.md @@ -370,7 +370,8 @@ const reduceCoinSupply = vendingMachine.reduceCoinSupply; tap.test('reduceCoinSupply([200, 100, 50, 10, 1], [100, 50, 10]); returns [200, 1]', function (t) { const result = reduceCoinSupply([200, 100, 50, 10, 1], [100, 50, 10]); const expected = [200, 1]; - t.deepEqual(result, expected); + t.deepEqual(result, expected, + 'reduceCoinSupply reduces the coinsAvail by the coins given as change'); t.end(); }); ``` @@ -447,7 +448,8 @@ tap.test('reduceCoinSupply remove more coins!', function (t) { ]; const result = reduceCoinSupply(COINS, remove); - t.deepEqual(result, expected); + t.deepEqual(result, expected, + 'reduceCoinSupply removes coins from supply of coinsAvail'); t.end(); }); ``` @@ -499,9 +501,11 @@ tap.test('Check Initial Supply of Coins in Vending Machine', function (t) { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]; - t.deepEqual(vendingMachine.getCoinsAvail(), COINS); + t.deepEqual(vendingMachine.getCoinsAvail(), COINS, + 'vendingMachine.getCoinsAvail() gets COINS available in Vending Machine'); vendingMachine.setCoinsAvail([1,2,3]); - t.deepEqual(vendingMachine.getCoinsAvail(), [1,2,3]); + t.deepEqual(vendingMachine.getCoinsAvail(), [1,2,3], + 'vendingMachine.setCoinsAvail allows us to set the COINS available'); t.end(); }); ``` @@ -519,7 +523,7 @@ you will see the last one fail: In `lib/vending-machine.js` file, after the `reduceCoinSupply` function definition, -add the following variable declaration: +add the following variable declaration and pair of functions: ```js // GOLBAL Coins Available Array. 10 of each type of coin. @@ -534,7 +538,6 @@ let COINS = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]; - /** * setCoinsAvail a "setter" for the COINS Array * @param {Array} coinsAvail the list of available coins @@ -549,10 +552,11 @@ function setCoinsAvail (coinsAvail) { function getCoinsAvail () { return COINS; } - ``` -We will _use_ the `COINS` array in the next step. +We will _use_ the `COINS` array +and both the "getter" and "setter" +in the next step. For now, simply _export_ "getter" and "setter" so the test will pass: ```js @@ -618,9 +622,12 @@ tap.test('sellProduct(215, [200, 100], COINS) returns [50, 20, 10, 5]', function const coinsPaid = [200, 100]; const result = vendingMachine.sellProduct(215, coinsPaid, COINS); const expected = [50, 20, 10, 5]; - t.deepEqual(result, expected); + t.deepEqual(result, expected, + 'sellProduct returns the correct coins as change'); + // check that the supply of COINS Available in the vendingMachine was reduced: - t.deepEqual(vendingMachine.getCoinsAvail(), reduceCoinSupply(COINS, result)); + t.deepEqual(vendingMachine.getCoinsAvail(), reduceCoinSupply(COINS, result), + 'running the sellProduct function reduces the COINS the vending machine'); t.end(); }); ``` @@ -653,7 +660,7 @@ If you run the tests: node test/vending-machine.test.js ``` -you will see the last one fail: +you will see the last one _fail_: ![sellProduct-test-fail](https://user-images.githubusercontent.com/194400/47678596-1bb44880-dbba-11e8-8927-892149a201bd.png)