1. Technology

Making Change

US $2 note

With this script you can enter the amount of change that you need to give to someone in your local currency into the makeChange() function (along with an array of the values of the coins and notes available in that currency) and the function will return an array containing a list of the coins and notes that you need to use to provide that change.

The first thing we need to do is to define what coins and notes are used in whichever currency we intend to use with our change maker script. We'll also want to define what we want to use for a decimal point (and number of decimal places), and also the currency symbol to use before or after each entry. I'll use Australian dollars in my example as that is what I am most familiar with.

var money = [.05,.1,.2,.5,1,2,5,10,20,50,100];
var decimals = 2;
var decpoint = '.';
var leadcurr = '$';
var trailcurr = '';

So I have defined that there are 5 cent, 10 cent, 20 cent, 50 cent, $1, $2, $5, $10, $20, $50, and $100 in the Australian currency (there is also a $200 but they are so rare that you wouldn't be giving one in change). I have defined two decimal places for the cents, to use a period for the decimal point and to have a $ in front of the amounts and no trailing currency symbol. Note that I have entered the amounts in from smallest to largest. The script has been set up to assume this order so as to reduce the amount of code needed.

Next we need a number format function that can take the amounts (which we entered as numbers) and format them appropriately to display as currency in our results.

The next thing we need are the two functions that we will use with the above information to actually work out the change. The getBiggest() function will extract the largest value that is not greater than a given value. We will call that repeatedly to select the largest coin or note that can be added to the change without going over the total change required. The makeChange() function is the one that actually does all the work. We simply pass it the amount of change we want to provide and our money array of coins and notes to select from and it will return an array containing the change required all nicely formatted ready to display. The function is also set up so that where the smallest coin is not the same as the smallest unit of currency (eg in Australia we scrapped all the one and two cent coins) the script will add an extra smallest coin to the result if the amount left at the end is over half the value of that coin (you may need to change the code if your country requires that the extra coin always be added rather than rounding to the closest result).

Here is the code for these two functions.

Now all you need to do is to provide a way to enter the amount of change to be given and provide a way to display the results returned in the array. I'll demonstrate the processing the simplest way by using a form consisting of one input field to enter the amount of change and a button to trigger the function call, and use a span in the page to display the results.

One way to make change for this amount is press button to find out.

Australian $20 note

©2014 About.com. All rights reserved.