Need Help with Cash Register project

Hi,

I am working on Cash Register project. My code passes all tests except test #3 -

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

Not sure why it is failing. In console log it shows the expected output for this test case. Please help.

Below is my code -

function checkCashRegister(price, cash, cid) {
  var changeBack = {};
  var currencyUnit = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
  var changeDue = cash - price;
  console.log("changeDue=" + changeDue);
  var cidTotal = cid.map(function(x){
    return x[1];
  }).reduce(function(a, b){
    return a + b;
  });
  cidTotal = cidTotal.toFixed(2);
  console.log("cidTotal=" + cidTotal);
  if(cidTotal < changeDue) {
    changeBack.status = "INSUFFICIENT_FUNDS";  
    changeBack.change = [];  
  } else if(cidTotal == changeDue){
    changeBack.status = "CLOSED";
    changeBack.change = cid;
  } else {
    var multiple = 0;
    var changeArr = [];
    for(var i=cid.length-1; i>=0; i--){
      if(changeDue >= currencyUnit[i]){
        if(changeDue < cid[i][1]) {
        multiple = parseInt(changeDue/currencyUnit[i]) * currencyUnit[i];
        changeDue = (changeDue % currencyUnit[i]).toFixed(2);
        changeArr.push([cid[i][0], multiple]);
        } else {
          changeDue = (changeDue - cid[i][1]).toFixed(2);
          changeArr.push([cid[i]]);
        }
      }

      } 
      if(changeDue == 0){
      changeBack.status = "OPEN";
      changeBack.change = changeArr;
      } else {
        changeBack.status = "INSUFFICIENT_FUNDS";
        changeBack.change = [];
      }
    }
  
  console.log(changeBack);

  // Here is your change, ma'am.
  return changeBack;
}

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

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

Running it on a fiddle, https://jsfiddle.net/snowMonkey/51afv2kL/5/, and i can see some of what’s going on – the values for the TWENTY and TEN are of a slightly different format from the rest of the array values. Looking into why, but feel free to play with that fiddle, see if it helps.

For the record, I strongly suggest commenting your code. It will help you later when you try to debug, and it will also help others to understand your thoughts.

1 Like

Here is what your code gives:

{status:"OPEN", change: [[["TWENTY",60]],[["TEN",20]],["FIVE",15],["ONE",1],["QUARTER",0.5],["DIME",0.2],["PENNY",0.04]]} 

And here is what is expected:

{status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}
1 Like

There is the issue.

[] instead of

YES! It’s a case of, when a value isn’t being used, it simply gets passed directly into the return value (in the else loop). it is being pushed [cid[i] ] and it will pass if it pushes [...cid[i] ] instead. Note the spread operator rather than the array passed into an array. Ouch.

1 Like

Hi @snowmonkey and @Tchoukoualeu,

I think I got where the issue is. Thank you both for your guidance. I really appreciate it.

Best of luck, hope it helped. :slight_smile:

@snowmonkey,
Thank you for the suggestion. I will keep that in mind.

Thanks a lot for finding the issue!

1 Like

Definitely :slight_smile: I have been stuck with this for a couple of days!