I used a really simple loop to solve this in a BlackJack game I'm writing in HTML5 using the Isogenic Game Engine. You can see a video of the BlackJack game which shows the chips that were used to make up a bet from the bet value on the BlackJack table above the cards: http://bit.ly/yUF6iw
In this example, betValue equals the total value that you wish to divide into "coins" or "chips" or whatever.
You can set the chipValues array items to whatever your coins or chips are worth. Make sure that the items are ordered from lowest value to highest value (penny, nickel, dime, quarter).
Here is the JavaScript:
// Set the total that we want to divide into chips
var betValue = 191;
// Set the chip values
var chipValues = [
1,
5,
10,
25
];
// Work out how many of each chip is required to make up the bet value
var tempBet = betValue;
var tempChips = [];
for (var i = chipValues.length - 1; i >= 0; i--) {
var chipValue = chipValues[i];
var divided = Math.floor(tempBet / chipValue);
if (divided >= 1) {
tempChips[i] = divided;
tempBet -= divided * chipValues[i];
}
if (tempBet == 0) { break; }
}
// Display the chips and how many of each make up the betValue
for (var i in tempChips) {
console.log(tempChips[i] + ' of ' + chipValues[i]);
}
You obviously don't need to do the last loop and it is only there to console.log the final array values.