Cash Register: Open tests

Not sure why my code isn’t passing the open tests. It works in the browser and when I console.log() in FCC, it shows the answer just fine. It’s only when I try to run the program that it says I didn’t pass the tests.

Tell us what’s happening:

Your code so far

var newCid = [
    ['ONE HUNDRED', 0.00],
    ['TWENTY', 0.00],
    ['TEN', 0.00],
    ['FIVE', 0.00],
    ['ONE', 0.00],
    ['QUARTER', 0.00],
    ['DIME', 0.00],
    ['NICKEL', 0.00],
    ['PENNY', 0.00]
]; 

function checkCashRegister(price, cash, cid) {
    var change = {};

    var cidTotal = Math.ceil((cid[0][1] + cid[1][1] + cid[2][1] + cid[3][1] + cid[4][1] + cid[5][1] + cid[6][1] + cid[7][1] + cid[8][1]) * 100) / 100;
    var register = cidTotal;
    var remainder = cash - price;
    
  
    if (register < remainder) {
      change = {status: "INSUFFICIENT_FUNDS", change: []}
    }

    if (register === remainder) {
      change = {status: "CLOSED", change: cid}
    }

    if(register > remainder) {
   
      while(register > 0 && remainder <= register) {  

            if(remainder >= 100 && cid[8][1] != 0) {
                remainder -= 100;
                cid[8][1] -= 100;
                newCid[0][1] += 100;
    
            }else if(remainder >= 20 && cid[7][1] != 0) {
                remainder = Math.ceil((remainder - 20) * 100) / 100;
                cid[7][1] -= 20;
                newCid[1][1] += 20;
    
            }else if(remainder >= 10 && cid[6][1] != 0) {
                remainder -= 10 
                cid[6][1] -= 10;
                newCid[2][1] += 10;
    
            }else if(remainder >= 5 && cid[5][1] != 0)  {
                remainder -= 5;
                cid[5][1] -= 5;
                newCid[3][1] += 5;

            }else if(remainder >= 1 && cid[4][1] != 0) {
                remainder -= 1;
                cid[4][1] -= 1;  
                newCid[4][1] += 1;
        
            }else if(remainder >= 0.25 && cid[3][1] != 0) {
                remainder -= 0.25;
                cid[3][1] -= 0.25;
                newCid[5][1] += 0.25;

            }else if(remainder >= 0.10 && cid[2][1] != 0) {
                remainder -= 0.10;
                cid[2][1] -= 0.10;
                newCid[6][1] += 0.10;

            }else if(remainder >= 0.05 && cid[1][1] != 0) {
                remainder -= 0.05;
                cid[1][1] -= 0.05;
                newCid[7][1] += 0.05;

            }else if(remainder >= 0.01 && cid[0][1] != 0) {
                remainder -= 0.01;
                cid[0][1] -= 0.01;
                newCid[8][1] += 0.01;

            }else {
                break;
            }
        }

        const result = newCid.filter(denom => denom[1] > 0);

        // if(register < 0 || remainder <= register ) {
        //     return {status: "INSUFFICIENT_FUNDS", change: []}
        // }

        change = {status: "OPEN", change: result};

    }

    return change;
}

//{"status":"OPEN","change":[["TWENTY",60],["TEN",20],["FIVE",15],["ONE",1],["QUARTER",0.5],["DIME",0.2],["PENNY",0.04]]}

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

Link to the challenge:

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
1 Like

It passed! Thanks so much!

I’m glad I could help. Happy coding!