JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:

I don’t understand why I’m failing this test. It is the only one I’m failing.

// running tests
checkCashRegister(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: }.

This is what I get when I console log it:

// console output
{ status: ‘INSUFFICIENT_FUNDS’, change: }

What am I missing?

Your code so far

function checkCashRegister(price, cash, cid) {
  let changeArr = [];
  let resultObj = {
  status: "",
  change: []
};
  let reversedCid = cid.reverse();
  let changeValue = [100, 20, 10, 5, 1, 0.25, 0.10, 0.05, 0.01]
  let changeDue = cash - price;
  const changeDueOrig = changeDue;
  
  let drawerTotal = 0;
  for (let i = 0; i < reversedCid.length; i++) {
    drawerTotal += reversedCid[i][1];
    //console.log(drawerTotal);
  }
    



  for (let i = 0; i <= reversedCid.length; i++) {
    if (changeDue >= changeValue[i] && changeDue > 0) {
      let changeCount = 0;
      let remainTotal = reversedCid[i][1];
      while (remainTotal > 0 && changeDue >= changeValue[i]) {
        remainTotal -= changeValue[i];
        changeDue -= changeValue[i];
        changeCount++;
        changeDue = changeDue.toFixed(2)
        //console.log(changeDue);
        //console.log(changeCount);
        
      }
      if (changeCount > 0) {
         changeArr.push([reversedCid[i][0], changeCount * changeValue[i]]);
         //console.log(changeArr);
      } 
    }
  }

  if (drawerTotal < changeDueOrig ) {
    resultObj.status = "INSUFFICIENT_FUNDS";
    
  } else if (drawerTotal === changeDueOrig) {
    resultObj.status = "CLOSED";
    resultObj.change = cid.reverse();
  } else {
    resultObj.status = "OPEN";
    resultObj.change = changeArr;
  }
  return resultObj;
  
}



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

console.log(checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]))
/*
console.log(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]]));


console.log(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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36

Challenge Information:

JavaScript Algorithms and Data Structures Projects - Cash Register

The log you have is not using the correct argument.

This is the test that is failing.

console.log(checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]))

Which logs

{ status: 'OPEN', change: [ [ 'PENNY', 0.01 ] ] }

And not as expected:

{status: "INSUFFICIENT_FUNDS", change: []}

Thanks,
I looked over it so many times my eyes just must have not been able to see it after a while. I realized I needed a conditional on the insufficient funds statement that checked if change due was still greater than 0.