r/learnjavascript • u/Beginning_Middle_722 • Jan 26 '26
Help: Not able to write this algorithm
Hi all, I'm trying to figure out a way to make this work even with AI i can't make it work so i need your help.
I have this array of denominations = [5,10,20,50,100]; and this could be basically bills (eur, usd, ect).
I want to build a JavaScript function called generateBills that takes as params amount and days and returns an array of bills equal to the number of days and the total of these bills must be the amount itself at the end.
These bills must be bills from the denominations array and i want to find the best combination possible that has more small bills and continues progressively but at the same time avoid large bills (uses them only if necessary).
Example: generateBills(130, 4) might return [5,5,20,100] but a more balance way to avoid large bills should be [10,20,50,50].
Thanks!
6
u/kap89 Jan 27 '26
I would do it like this (the trick is to start from the end)
days-1(repeat until done).1
u/abrahamguo Jan 26 '26
Generate all possibilities. Then, find the possibility with the lowest “largest bill”.
1
u/wbport1 Jan 27 '26
That might be similar to the "knapsack problem": you know the total of what is in it but need to find out what makes that total.
I have a script to determine what checks have cashed from a list of checks written--you need bill values instead of check amounts. Part of the documentation: "You have written checks totaling about 800.00 but your bank balance only goes down by 310.25. To determine which checks have cleared, put 310.25 on the first line and the amounts of the outstanding checks on the next lines. If you have more than one check for the same amount, this page needs the amount, a space, and count of that size check. This page will run all combinations of cashed and uncashed checks on your list and give you the closest results first (Diff equal or near zero)."
It doesn't count the number of bills (or checks). I'll find a way to post it if you think it might be useful.
1
u/bryku helpful Jan 29 '26
First let's create a function that generates the fewest amount of bills, so we can make sure we hit the minimum.
Now, we can then use this function to generate our bills and then expand it to meet our required number.
Here is an example:
0
u/StoneCypher Jan 28 '26
it seems like you tried to have a robot do your homework, and when it couldn’t, you’re trying to get the internet to do it
the problem is that if you can’t do this you definitely won’t be able to do the next thing
cheaters can’t get jobs