So I've been trying to create a program in Javascript/jQuery that splits an amount of money into the smallest amount of dollar bills. So far the program only works with one bill, and I'm not too sure how to implement the rest and need a push in the right direction here.
var bills = [5, 10, 20, 50, 100];
while(money > 0){ // Keep deviding
for(var i=0; i < bills.length; i++){
if(money < bills[i])
return "You need a $" + bills[i] + " bill to pay for your item.";
}
}
If I run this with money = 89; it will return 100 because that's the closest bill that can pay for $89, but I want it to return 50 + 20 + 20 so it will work with money = *anything*.
EDIT: After comments I've now come so far:
while(money > 0){ // Keep deviding
for(var i=bills.length-1; i >= 0; i--){
if(money > bills[i] || i == 0){
stringToReturn += " + $" + bills[i];
money -= bills[i];
break;
}
}
}
$50 + $20 + $20 + $5 + $5rather than $100 if the input is 99. Will update my post with my current code.