Cash Register quick help

I don’t how to check for the suitable coin in the array and then obtain the change from highest to lowest order.

function checkCashRegister(price, cash, cid) {

  let change = cash - price;

  let allcash = 0;

  for(var i = 0; i < cid.length; i++) {

     allcash += cid[i][1];

    }

   if(allcash < change) {

     return {status: "INSUFFICIENT_FUNDS", change: []};

     }

   for( var j = 0; j < cid.length; j++) {

     if(allcash == cid[j][1] ) {

     return {status: "CLOSED", change: cid};

  }


   }

}

checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);

Respectfully, that’s kind of the point of the exercise. You’ll have to do more than just compare total change owed and cash in drawer. You need a way to see if the change will fit, which requires some method of storing the change by denomination and total quantity, and (a) function(s) that can figure out if the cash in drawer can make the change. So you have to think about data structures (do you want object(s)? array(s)?) and how to structure them so that your function can work.

1 Like

Hello!

It seems that you’re having problems with your problem solving skills so, the first thing I would recommend is to search online for how to improve it. Check out all the problem solving articles on the Free Code Camp Blog.

After that, take a look at websites like hackerrank.com, which are sites with lots of problems to solve (and you can choose the language you want to work with).

Finally, do not give up! There’s no problem hard enough that cannot be solved!

May be a phrase well known by most developers. This means that you should split the problem into smaller pieces and solve each one. It’s not something to be grasped in a minute; it may take time (hours, days, etc., everyone has their own times), but once you get it you will see that every problem seems easier :stuck_out_tongue:.