Code Showing Correct Output, But Not Passing (Cash Register Challenge)

I have been trying to figure out what is going on with my code and I haven’t been able to. It appears to be returning the required answers, but it isn’t passing any of the tests. It is an object like the challenge prompt says and everything. Am I mission something?

function checkCashRegister(price, cash, cid) {
  //Calculate the difference to return amount of change due
  let diff = cash - price;

  //The three output object possibilities to return later
  let returns = 
    [
      {status: "CLOSED", change: []},
      {status: "INSUFFICIENT_FUNDS", change: []},
      {status: "OPEN", change: []}
    ];

  //The monetary values of each coin or bill up to $100
  const monValue = 
    [
      ["PENNY", 0.01],
      ["NICKEL", 0.05],
      ["DIME", 0.1],
      ["QUARTER", 0.25],
      ["ONE", 1],
      ["FIVE", 5],
      ["TEN", 10],
      ["TWENTY", 20],
      ["ONE HUNDRED", 100]
    ];
  //if no change is due return object 0 from returns array
  if (diff == 0) {
    return returns[0];
  } else {
    //else loop through ever possible monetary value for $100 to one and subtract it from cash register amount and the amount of change due
    for (let i = monValue.length-1; i >= 0; i--) {
      //loop throught each montary amount until that amount can't be subtracted anymore or return that there is not enought change in the register
      while (true) {
        if (diff - monValue[i][1] >= 0){
          if (cid[i][1] - monValue[i][1] < 0) {
            return returns[1];
          } else {
            diff -= monValue[i][1];
            cid[i][1] -= monValue[i][1];
            returns[2].change.push(monValue[i]);
          }
        } else  {
          break;
        }
      }
    }
    //return the return object 2 in the array with the new change amounts
    return returns[2];
  }
}

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

This is what I do to debug FCC’s problem. Copy pasting the spec, comment out the “should”, and console.log the case before the “should”. Copy paste this block at the very bottom and click run the test, see what you missed. I am amazed you can go this far without this kind of debugging.

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", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])); // should return an object.

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]]))// should return {status: "OPEN", change: [["QUARTER", 0.5]]}.

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]])) // should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}.

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]])) // should return {status: "INSUFFICIENT_FUNDS", change: []}.

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]])) // should return {status: "INSUFFICIENT_FUNDS", change: []}.

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

Okay, I finally figured it out. Thank you for the console.log debugging idea. There were a lot of elements that needed to be changed or add to the code that I hadn’t realized because of this. Normally I just have one console.log line at the bottom until it passes and then I copy and paste any subsequent specs that failed until they all pass. I never looked past the first spec test this time because I couldn’t even figure the one out. Thanks for the suggestion. It is the only way I would have known what was wrong with the first test. It seems obvious now. Thanks!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.