javascript question. HELP ME!
by - Thursday, January 1, 1970 at 12:00 AM
A customer is having problems due to lack of coins and needs to optimize change,
we need to add a function in your system to help calculate usage
few coins possible to return change. Develop in JavaScript
a logic to calculate a certain value in as few coins as possible. using
HTML provides a field to enter the value to be calculated and a button
to perform the calculation. When you press the button, it should do the calculation and show the
result with total coins and division by values used between 50, 25, 10, 5
and 1 cent. As an example, after typing 2.36 the expected result should be:
Total coins: 5
To be:
4 of 50 cents
1 of 25 cents
1 out of 10 cents
0 out of 5 cents
1 of 1 cent
☠ fuck ☠
Reply
(August 25, 2022, 10:39 PM)dior Wrote: why does this sound like a school question


it's for my college
☠ fuck ☠
Reply
(August 25, 2022, 10:39 PM)dior Wrote: why does this sound like a school question


Yes.. it's easy
When I was 15 years old I can solve this problem only 4 minutes
Reply
Are you looking for someone to just do it for you?

What have you been trying so far? Is there a syntax issue youre running into or are you having a hard time with the actual algorithm? Post what code you have so far.
Reply
solution seems to be a mixture of modulo / div..

take the amount...div through the highest coin value (2.36/.50 => 4), keep rest => .36, div again to the next highest value => 0.36/.25 => 1, keep rest => .11 .. and so on
Reply
(August 26, 2022, 08:07 AM)trollinator321 Wrote: solution seems to be a mixture of modulo / div..

take the amount...div through the highest coin value (2.36/.50 => 4), keep rest => .36, div again to the next highest value => 0.36/.25 => 1, keep rest => .11 .. and so on

this I think will also work.

this seem like Knapsack. 

A general known puesdo code.  


func count( n, m )

  for i from 0 to  n+1
    for j from 0 to m
      if i equals 0
        table[i, j] = 1         
      else if j equals 0
        if i%S[j] equals 0
              table[i, j] = 1 
        else
              table[i, j] = 0;
      else if S[j] greater than i
        table[i, j] = table[i, j - 1]
      else
        table[i, j] = table[i - S[j], j] + table[i, j-1]

  return table[n, m-1]



Implement it in JavaScript , this is your work not mine.
Those who share kindness, I will repay that payment 10-fold, and Who do injustice, try to hurt the innocent, I will repay that injustice a 1000 times over.
Reply
because coin values are nice you can just greedy keep using the largest coin

if coin values were not nice you would need to use dynamic programming like mysterious example above
Reply


 Users viewing this thread: javascript question. HELP ME!: No users currently viewing.