Cash Register last javascript algorithm

Tell us what’s happening:
I don’t know how to even begin traversing the cid variable to grab the currencies as well as grabbing only the amount needed for that particular currency when attempting to give them the change. How do I know the point at which I cannot give them the exact change although the amount in register exceeds the change due.

Your code so far


function checkCashRegister(price, cash, cid) {
  var change = (cash - price).toFixed(2)
  // Here is your change, ma'am.\
  var totalInRegister =[]
  cid.forEach(element => totalInRegister.push(element[1]))
  totalInRegister = totalInRegister.reduce((x, y) => x+y).toFixed(2);


  if(change === totalInRegister){
    return {status: 'CLOSED', change: cid}
  } else if (change > totalInRegister){
    return {status: 'INSUFFICIENT_FUNDS', change: []}
  }

var currencyValue = {
    "PENNY": 0.01,
    "NICKEL": 0.05,
    "DIME": 0.10,
    "QUARTER": 0.25,
    "ONE": 1.0,
    "FIVE": 5.0,
    "TEN": 10.0,
    "TWENTY": 20.0,
    "ONE HUNDRED": 100.0
  };

  // I want to traverse the cid variable in reverse order and push each value respectively since this is what the requirements ask for
  console.log(cid.reverse())
  // This will be my array that I display if status is OPEN
  var coinsBills = []
  // I'm going to need to push values onto coinsBills in their respective amounts(TENS, FIVE, ONE) but never exceeding the amount for each currency in the associated cid variable. Give Customer the most consolidated change at all times

}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]

checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register

When you have tried making change with all the denominations in the drawer but change is still owed to the customer.

Just like a cashier does. If they owe a customer $80 they don’t pay out any hundreds - they count out twenties - 20, 40, 60, 80. If they run out of twenties they start dispensing tens instead. Every count decreases the amount owed to the customer and decreases the amount in their drawer. You will have to do something similar. You’ll also have to keep track of how much of each denomination you pay out for the change property on the returned object.

They’re given in the same order every time, none are skipped and each one has a unique name as a first element. There are some possibilities.

This challenge can take some time. Your first working solution may not be very elegant. There are quite a few ledgers to keep current - how much owed, how much of each currency was paid out, how much of each currency you still have in the drawer.

There are some things you do not have to worry about.

The money the customer pays to you does not really affect your drawer because you are only returning the change you paid out and each problem starts with a new drawer.

There are no weird test cases to trip you up like if change were .55 and you paid out two quarters already but had lots of dimes but no nickels. Technically you could have still made change from your drawer with one quarter and three dimes but that is beyond the level of this challenge.

Good luck!