Cash Register flyin

I’m trying to understand why the order of the array changed or needed to change in the last validation checks. I want to write my own code rather then copy or emulate the hint but this order of the output array makes this difficult. Any help with this would be appreciated.

Your code so far


function checkCashRegister(price, cash, cid) {
  var money = [
    {name: "ONE HUNDRED", value: 100.00},
    {name: "TWENTY", value: 20.00},
    {name: "TEN", value: 10.00},
    {name: "FIVE", value: 5.00},
    {name: "ONE", value: 1.00},
    {name: "QUARTER", value: 0.25},
    {name: "DIME", value: 0.10},
    {name: "NICKEL", value: 0.05},
    {name: "PENNY", value: 0.01}
  ];
  let tc = 0;
  let aArr = [];
  let cntcid = 0;
  let cL = cid.length-1;
  let change = cash - price;
  
  for (let i=0;i<cid.length;i++) {
    cntcid = cntcid + cid[cL-i][1];
    money[i].total = cid[cL-i][1];
  }

  function Change() {
    this.status = "";
    this.change = [];
  }
  
  let newC = new Change();
  console.log(change);
  console.log(cntcid);
  if (cntcid < change) {
    newC.status = "INSUFFICIENT_FUNDS";
    return newC;
  } else if (cntcid === change) {
    newC.status = "CLOSED";
  } else {
    newC.status = "OPEN";
  }
  
  for (let j=0;j<money.length;j++) {
    var a = money[j];
    aArr=[];
    if (change/a.value >= 1) {
      tc = change/a.value - (change/a.value)%1;
      if (tc*a.value<=a.total) {
        change = +(change - tc*a.value).toFixed(2);
        aArr.push(a.name);
        aArr[1] = +(tc*a.value).toFixed(2);
      } else {
        change = +(change - a.total).toFixed(2);
        aArr.push(a.name);
        aArr[1] = a.total;
      }
        newC.change.push(aArr);
    } 
  }

  console.log(change);
  if (change!=0.00) {
    newC.status = "INSUFFICIENT FUNDS";
    newC.change = [];
  }
  return newC;
}



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]]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0.

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

In the first checks the change output is

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]]}.

The last checks are:

checkCashRegister(19.5, 20, [[“PENNY”, 0.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]) should return {status: “CLOSED”, change: [[“PENNY”, 0.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]}.

So the array values are descending in the first checks and ascending in the last checks.

I’m talking about the change property arrays in the returned objects.

So you mean if the status is open the “should return” is shown as descending and other cases show as ascending? Interesting.

Have you tried altering the order of the change array to see if it fails the test?

It could be that the order does not matter or maybe the test for that challenge is based on a faulty assumption.

In my solution in the case of open status I counted back change largest to smallest bills and other cases I just returned the cash drawer that was passed as a parameter - so smallest denomination to largest. That just happened to match their “should return” nicely.

Your mission if you choose to accept it is to determine if altering order of denomination in change array fails the tests

@alhazen1
I see how it wouldn’t be a big change to just return the input as the change output. That seems like an acceptable solution but I would think the check would take it either way.

OK - curiosity was killing me so I tried the challenge the other way and it failed.

I guess you could open an issue on the FCC github

@alhazen1 @camperextraordinaire

I am also getting an error on the 5th check when my output to the console is showing the correct values.

Any ideas?

You left off the underscore in INSUFFICIENT_FUNDS

Ahhhhhhh!!! Good catch.

Thank you!!