Cash Register - Help needed

Tell us what’s happening:
Hey guys, trying to finish up my last javascript challenge but having some issues with the final test case (when it should return status: “CLOSED”, change: […]). My output appears correct and the code passes all the other tests - I can’t quite figure out whats causing it to fail. Can anyone advise?

Your code so far


function checkCashRegister(price, cash, cid) {
  var changeVal = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
  var changeNames = ["PENNY", "NICKEL", "DIME", "QUATER", "DOLLAR", "FIVE", "TEN", "TWENTY", "ONE HUNDRED"];
  var totalCID = 0;
  var changeReq = cash - price;
  var output = {status: "", change: []}

  // Total Cash in Drawer;
  for (let i = 0; i < cid.length; i++) {
    totalCID += cid[i][1];
  }
  totalCID = (Math.round(totalCID * 100)) / 100;
  
  // If not enough cash in drawer, return insufficient
  if (totalCID < (cash - price)) {
    output.status = "INSUFFICIENT_FUNDS";
    return output;
  } else {
    // Work out the change due
    for (let i = cid.length - 1; i >= 0; i--) {
      let changeToAdd = 0;
      let changeObj = [cid[i][0]];
      //console.log(cid[i][1] + " - " + changeVal[i]);
      //console.log(changeReq + " > " + changeVal[i] + " CID " + cid[i]);
      if ((changeReq >= changeVal[i]) && (cid[i][1] > 0 )) {
        while ((changeReq >= changeVal[i]) && (cid[i][1] > 0)) {
          changeReq -= changeVal[i];
          changeToAdd += changeVal[i];
          cid[i][1] -= changeVal[i];
          totalCID -= changeVal[i];
          totalCID = Math.round(totalCID * 100) / 100;
          cid[i][1] = Math.round(cid[i][1] * 100) / 100;
          changeReq = Math.round(changeReq * 100) / 100;   
        }
      }
   
      if (changeToAdd > 0) {
        changeToAdd = Math.round(changeToAdd * 100) / 100;
        changeObj.push(changeToAdd);
        output.change.push(changeObj);
      } 
    }
  }
  
  // If not able to return correct change
  if (changeReq != 0) {
    output.status = "INSUFFICIENT_FUNDS";  
    output.change = [];
  } else {
    // If total cash required as change, set status to CLOSED + add extra
    if (totalCID == 0) {
      output.status = "CLOSED";
      for (let j = 0; j < changeNames.length; j++) {
        let checker = 0;
        for (let x = 0; x < output.change.length; x++) {
          if (output.change[x][0] == changeNames[j]) {
            checker++
          }
        }
        if (checker == 0) {
          output.change.push([changeNames[j], 0]);
        }
      }
    } else {
      // Else set status to open, leave change as is
      output.status = "OPEN";
    }
  }
  
  console.log(output.status);
  console.log(output.change);
  return output;
}

// 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", 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/71.0.3578.98 Safari/537.36.

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

You have two typos:

var changeNames = ["PENNY", "NICKEL", "DIME", "QUATER", "DOLLAR", "FIVE", "TEN", "TWENTY", "ONE HUNDRED"];

"QUATER" should be "QUARTER" and "DOLLAR" should be "ONE".

Yes, the challenge notes do say that a dollar is called "DOLLAR", but for some reason the tests expect it to be called "ONE".

1 Like

Omg thank you so much, I can’t believe I missed that!