Skip to content

Commit

Permalink
I know some mocha and I wrote a fun recursive change calculator
Browse files Browse the repository at this point in the history
I know I’m “supposed to” use arrays and mutation but I went the
functional route.
  • Loading branch information
holmesworcester committed Mar 3, 2017
1 parent 885dddb commit 19f3671
Show file tree
Hide file tree
Showing 252 changed files with 45,329 additions and 0 deletions.
29 changes: 29 additions & 0 deletions cash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* totalPayable = 210 // £2.10
cashPaid = 300 // £3.00
dfference = 90 // 90p
change = [50,20,20] // 50p, 20p, 20p */

var _ = require("underscore");
var coins = [5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 5, 2, 1];
var C = {};

C.getChange = function (totalPayable, cashPaid) {
'use strict';
// a function that consumes a value and an array of Coin sizes, in order from largest to smallest
// returns the minimal combination of coins for the given value
var getCoins = function (amount, coinsSoFar, coinsAvailable) {
var biggestCoin = coinsAvailable[0]; // the first Coin in the list will be the biggest.
if (amount == 0) { // we've already taken care of all the coins and they're in our array, just return it.
return coinsSoFar;
}
else if (amount >= biggestCoin) { // we're definitely going to use the biggest coin, maybe bringing us to zero if it's equal to the amount.
return getCoins (amount - biggestCoin, coinsSoFar.concat([biggestCoin]), (coinsAvailable));
}
else { // the biggest coin is too big so throw it out and try again
return getCoins (amount, coinsSoFar, _.rest(coinsAvailable));
}
};
// now substract the amount, give getCoins an empty array and the constant "coins"
return getCoins (cashPaid - totalPayable, [], coins);
};
module.exports = C;
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var C = require("./cash.js");

console.log(C.getChange(121, 5000));
1 change: 1 addition & 0 deletions node_modules/.bin/_mocha

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/mkdirp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/mocha

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions node_modules/balanced-match/.npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions node_modules/balanced-match/LICENSE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions node_modules/balanced-match/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions node_modules/balanced-match/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 110 additions & 0 deletions node_modules/balanced-match/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 19f3671

Please sign in to comment.