Cash Register -- Need help

Can someone help me with this challenge.It is by far the most difficult one I have came across.
I have passed all the challenges but two

***checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]) should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}.***

***This text will be hiddencheckCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "INSUFFICIENT_FUNDS", change: []}.***

I really need some help here . This is by far the most difficult thing I have ever experienced in coding.
This is my code so far:

const statusDictionary = {
  closed : "CLOSED",
  insufficent : "INSUFFICIENT_FUNDS",
  open : "OPEN",
};

function checkCashRegister(price, cash, cid) {
  
  var output = {status : "", change : cid};
  var change = cash - price;
  var cashAvailable = cashAv(cid);
  if (cashAvailable < change){
    output.status = statusDictionary.insufficent;
    output.change = [];
  }
  else if (cashAvailable == change){
    output.status = statusDictionary.closed;
    output.change = [...cid];
  }
  else if (cashAvailable > change){
      output.status = statusDictionary.open;
      const denominations = [
        {"name":"PENNY","value":0.01},
        {"name":"NICKEL","value":0.05},
        {"name":"DIME","value":0.10},
        {"name":"QUARTER","value":0.25},
        {"name":"ONE","value":1},
        {"name":"FIVE","value":5},
        {"name":"TEN","value":10},
        {"name":"TWENTY","value":20},
        {"name":"HUNDRED","value":100}]
      var answer = [];
      var tracker = 0;
       for(let i = cid.length - 1; i >= 0; i--){
    while(change >= denominations[i].value  && tracker < cid[i][1]){
      change  -= denominations[i].value
      tracker += denominations[i].value
      } if(tracker > 0){
    answer.push([denominations[i].name, tracker]);
  } tracker = 0;
  output.change = answer;

}
      }
  return output;
};

function cashAv (cid) {
  let available = 0;
  for (let i = 0; i < cid.length; i++){
    available += cid[i][1]

    };
  
  return available.toFixed(2);
};




console.log(JSON.stringify(checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])));

Someone please look at my code and guide me what is wrong in it.

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

You may having a floating point error. Try multyplying everything by 100 at the beginning so to have everything in units and no decimals, and then at the end you convert again to decimals

1 Like

You can’t just add up all the money in your cash register in cashAv into one number variable and make that comparison to find out if you give the change or not. In the example of a failing test you have one penny coin and one dollar bill. The change that you need to give is 50 cents. How can you give a change of 50 cents if all you’ve got in your register is a dollar bill and one cent coint?? Yet your logic says you can and goes forward, trying co calculate the change that you can’t logically come up with IRL.

Yeah I figured that out but can not figure how to deal with it.
Anyways thanks.

Thank you so much . This just helped me clear one more condition.

As an option you can keep substracting money from the cash register until you run out of money. You are already doing something similar, you just need to add a check in the right place for when you run out of money in your register before you get all the change that you owe the customer.

Running out of money, for instance, would mean that you went to the last denomination available ( pennies in this case ) and the value of that key-value pair in your object is 0 and the “money you owe to the customer” is still greater than 0.

Spoiler alert - it’s not going to be in the beginning, before you start going through the register. It will probably be towards the end of your loop, as one of the conditions for the loop termination :stuck_out_tongue:

Awesome. That was exactly what I had to do. Trust me I can really not put into words how happy I am today because of you.

1 Like