Javascript Programmer, Data Analyst and Mathematician. I have had a keen interest in all things software and hardware since the age of 5, with hands on programming experience age 11. I once even got myself suspended for exploiting a vulnerability in my high schools computer network.
Currently living in Ipswich, England. I have a rich portfolio of interactive websites and API's. I have over 5 years professional experience in web development and many more years of non professional experience.
Below is a map of my geographical influence from the last 25 years.
Alternatively click here to view the scalable map.
You can also find me on GitHub. Click octocat below to check out some of my awesome repositories.
If you would like to keep seeing me doing what I love you can show your support by sending bitcoin to this address:
1Joz42y34GBMFwA8zVfbYqC8H3rQpuLfpJ
Below are 2 functions that will return the optimal amount of change for a given amount out of £1, 50p, 20p, 10p, 5p, 2p and 1p coins. The first function returns the optimal amount, whereas the second function returns the optimal amount from a finite amount of coins, here defined in the array 'quantities'.
If there are not enough coins to make up the desired amount the function will not return any coins and it will not remove any remaining coins, instead it will just print the message 'insufficient coinage'.
JavaScript Code
var coins = [100, 50, 20, 10, 5, 2, 1];
var quantities = [11, 24, 0, 99, 5, 2, 11];
var unlimitedCoins = function(amount) {
var change = [];
var total = 0;
coins.forEach(function(coin) {
while (total + coin <= amount) {
change.push(coin);
total += coin;
}
});
return change;
};
var limitedCoins = function(amount) {
var change = [];
var total = 0;
var origionalQuantity = _.clone(quantities);
coins.forEach(function(coin, coinIndex) {
while (total + coin <= amount && quantities[coinIndex] > 0) {
quantities[coinIndex]--;
change.push(coin)
total += coin;
}
});
var sum = 0;
change.forEach(function(coin){
sum += coin;
});
if (sum != amount) {
quantities = origionalQuantity;
console.log("insufficient coinage");
return [];
}
return change;
}
To view a live demo of this code in action click here