[SOLVED] Cash Register Bad Test Case

Tell us what’s happening:
Looking at the last test case for this challenge.

The last test case states that the result should show “CLOSED”, but based on the given inputs to the function, I fail to see how the expected output can be reached, based on the information given for the challenge. Can somebody double check for me whether I’m blind?

The last test case states that given a price of 19.5 and a cash value of 20, I should receive notice that the cash register is closed. But the remaining money in the register does not equal 0.50.

Here is the test case:

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

Your code so far


var tenderValues = {
  "ONE HUNDRED": 100.00,
  "TWENTY": 20.00,
  "TEN": 10.00,
  "FIVE": 5.00,
  "ONE": 1.00,
  "QUARTER": 0.25,
  "DIME": 0.10,
  "NICKEL": 0.05,
  "PENNY": 0.01
}

function checkCashRegister(price, cash, cid) {
  let change = cash - price;
  let status = "CLOSED";
  let returned = [];

  // Here is your change, ma'am.

  let index = cid.length - 1
  while(index >= 0 && change > 0) {
    let tenderPair = cid[index];
    let tenderName = tenderPair[0];
    let tenderStores = tenderPair[1];
    let tenderValue = tenderValues[tenderName];
    let tenderReturned = 0;

    while(change >= tenderValue && tenderStores > 0){
      tenderStores -= tenderValue;
      tenderStores = Number.parseFloat(tenderStores.toFixed(2));
      change -= tenderValue;
      change = Number.parseFloat(change.toFixed(2));
      tenderReturned += tenderValue;
      tenderReturned = Number.parseFloat(tenderReturned.toFixed(2));

      console.log("tenderStores " + tenderStores);
      console.log("change " + change);
      console.log("tenderReturned " + tenderReturned);
    }

    if(tenderReturned > 0){
      returned.push([tenderName, tenderReturned]);
    }

    if(tenderStores > 0) {
      status = "OPEN";
    }
    index--;
  }

console.log(returned);

  if(change > 0){
    status = "INSUFFICIENT_FUNDS";
    returned = [];
  }
  
  if(status == "CLOSED"){
   return {"status": status, "change": cid}
  }

  return {"status": status, "change": returned};
}

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

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

1 Like

because the exact change required is the exact amount in the drawer…

If something costs 50 cents and you give $20, after the transaction is complete, I would be giving you 50 cents in change which would close out my register.

1 Like

The test case does not provide 2 quarters, it has 5 pennies.

Nevermind, I didn’t read carefully. You’re right. I thought it had .05 pennies worth.

I also shouldn’t forget to say thanks! Thanks for pointing my misreading.

Yeah thanks. I misread thinking it was .05 instead .5. Was on a challenge marathon.