Cash Register: Function returning correct outcomes but tests still failing

Tell us what’s happening:

For the second and third test, the function is returning precisely what it needs to, but the tests still fail to pass on both chrome and firefox. Why am I able to console.log the correct returns but not able to pass the tests here?

Your code so far


const currencyValuesArr = [0.01,0.05,0.1,0.25,1,5,10,20,100];
function checkCashRegister(price, cash, cid) {
  //get total money contents of drawer
    let cidTotal = 0;
    let changeDue = [["PENNY", 0],
                     ["NICKEL", 0],
                     ["DIME", 0],
                     ["QUARTER", 0],
                     ["ONE", 0],
                     ["FIVE", 0],
                     ["TEN", 0],
                     ["TWENTY", 0],
                     ["ONE HUNDRED", 0]];
    let finalChangeDue = [];                 
    for (let i = 0; i<cid.length;i++) {
      cidTotal += cid[i][1];
    }
    cidTotal = Math.round(cidTotal *100)/100;
    let change = cash-price;
      for (let j=8; j>=0;j--) {
        while (change >= currencyValuesArr[j] && cid[j][1] >=currencyValuesArr[j]) {
          cid[j][1] -= currencyValuesArr[j];
          change = change- currencyValuesArr[j];
          change = Math.round(change*100)/100;
          changeDue[j][1] += currencyValuesArr[j];
        }
      }
      for (let k=0;k<changeDue.length; k++) {
        let intermediateArr = [];
        if (changeDue[k][1] !== 0) {
          
          intermediateArr.push(changeDue[k]);
          finalChangeDue.push(intermediateArr);
        }
      }
      console.log(change);
      return (change==0 ?
        {status: "OPEN", change:finalChangeDue.reverse()}: 
        {status :"INSUFFICIENT_FUNDS", change:[]});
}  

console.log(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]]));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

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

Out of interest, what is cidTotal meant to be doing? It doesn’t actually do anything right now, it’s just assigned to for some reason so that loop isn’t really needed

Also for some reason you have an extra layer to your returned array

Instead of [['Twenty': 60], [...]] etc, you return [[['Twenty': 60 ]], [[...]]] etc

1 Like

The cidTotal is for the last test, in which the cash in drawer exactly matches the change due.

Originally, my function was returning a 1d array when it needs a 2d. Thank you for pointing out that it had 3 layers, please accept my green check-mark of appreciation. Now I’ve passed all tests with the following code:

const currencyValuesArr = [0.01,0.05,0.1,0.25,1,5,10,20,100];
function checkCashRegister(price, cash, cid) {
let cidTotal = 0;
let change = cash-price;
let changeDue = [[“PENNY”, 0],[“NICKEL”, 0],[“DIME”, 0],[“QUARTER”, 0],[“ONE”, 0],[“FIVE”, 0], [“TEN”, 0],[“TWENTY”, 0], [“ONE HUNDRED”, 0]]; let finalChangeDue = [];
for (let i = 0; i<cid.length;i++) {
cidTotal += cid[i][1];
}
cidTotal = Math.round(cidTotal 100)/100;
if (cidTotal === change) {
return {status: “CLOSED”, change: cid};
}
for (let j=8; j>=0;j–) {
while (change >= currencyValuesArr[j] && cid[j][1] >=currencyValuesArr[j]) {
cid[j][1] -= currencyValuesArr[j];
change = change- currencyValuesArr[j];
change = Math.round(change
100)/100;
changeDue[j][1] += currencyValuesArr[j];
}
}
for (let k=0;k<changeDue.length; k++) {
let intermediateArr = [];
if (changeDue[k][1] !== 0) {

      finalChangeDue.push(changeDue[k]);
    }
  }
  return (change==0 ?
    {status: "OPEN", change:finalChangeDue.reverse()}: 
    {status :"INSUFFICIENT_FUNDS", change:[]});

}