CashRegister, Failing "OPEN"

Hello i’ve been working on the CashRegister Alorithm for a while now. I have a problem i can’t figure out. My Code works just fine. However i fail when i copy the code from Visual Studio Code to the free Code camp application. I’ve created some tests for the function. I fail all “OPEN” however in my dev environment everthing works perfectly!


let change = {
    status: "OPEN",
    change: []
};

function checkCashRegister(price, cash, cid) {
    let cidCopy = JSON.parse(JSON.stringify(cid));
    let currencyAmmount = [
        ["PENNY", 0.01],
        ["NICKEL", 0.05],
        ["DIME", 0.1],
        ["QUARTER", 0.25],
        ["ONE", 1],
        ["FIVE", 5],
        ["TEN", 10],
        ["TWENTY", 20],
        ["ONE HUNDRED", 100]
    ];
    let dept = cash - price;


    for (let coin = cid.length - 1; coin >= 0; coin--) {
        while (dept > 0 & dept >= currencyAmmount[coin][1] & cid[coin][1] !== 0) {
            if (cid[coin][1] !== 0) {
                let coinValue = currencyAmmount[coin][1];
                cid[coin][1] -= coinValue;
                cid[coin][1] = Math.round(cid[coin][1]*100)/100;
                dept -= coinValue;
                dept = Math.round(dept * 100) / 100;
                addToChange([currencyAmmount[coin][0], currencyAmmount[coin][1]]);
            }
        }
    }
    if (dept != 0) {
        change.status = "INSUFFICIENT_FUNDS";
        change.change = [];
    } else if (cashInDrawer(cid)) {
        change.status = "OPEN";
        
    } else {
        change.status = "CLOSED";
        change.change = cidCopy;
    }
    return change;
}
// cic = cash in change
function addToChange(coinToAdd) {
    if (change.change.length > 0) {
        for (let cic = 0; cic < change.change.length; cic++) {
            if (coinToAdd[0] === change.change[cic][0]) {
                change.change[cic][1] += coinToAdd[1];
                //console.log(change.change[cic][1]);
                change.change[cic][1] = Math.round(change.change[cic][1] * 100) / 100;
                //console.log(change.change[cic][1]);
                break;
            } else {
                if (cic == change.change.length - 1) {
                    change.change.push(coinToAdd);
                    break;
                }
            }
        }

    } else {
        change.change.push(coinToAdd);
    }
}

function cashInDrawer(cid) {
    for (let i = 0; i < cid.length; i++) {
        if (cid[i][1] != 0) {
            return true;
        }
    }
    return false;
}

This are the tests:

//TESTs
//let what = checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
//let solution =  {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]};
//let what = 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]])
//let solution = { status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]] }
let what = 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]]);
let solution = { status: "OPEN", change: [["QUARTER", 0.5]] };
//let what = checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
//let solution = {status: "OPEN", change: [["PENNY", 0.5]]};


function testAddToCash() {
    addToChange(["PENNY", 0.01]);
    addToChange(["PENNY", 0.01]);
    addToChange(["ONE HUNDRED", 100]);
    addToChange(["PENNY", 0.01]);
    addToChange(["ONE HUNDRED", 100]);

    console.log(change.change)
    console.log("[ [ 'PENNY', 0.03 ], [ 'ONE HUNDRED', 200 ] ]");
    if (JSON.stringify(change.change) == JSON.stringify([['PENNY', 0.03], ['ONE HUNDRED', 200]])) {
        return "Works just fine!"
    }
    return "Wroooong!"

}


function testCashRegister(what, solution) {
    console.log(what);
    console.log(solution);
    if (JSON.stringify(what) == JSON.stringify(solution)) {
        return "yey";
    }
    return "ney";
}

console.log(testCashRegister(what, solution));
//console.log(testAddToCash());

Have you actually done any debugging using the console? I added a line to show me what your function sends back to the caller, and it doesn’t seem to be returning an accurate value.

Here’s what I added, just before your return change; in the main checkCashRegister():

    console.log("for ",price,", ",cash,", and ",JSON.parse(JSON.stringify(cid)),", returning: ",JSON.parse(JSON.stringify(change)) );

Note that that doesn’t fix anything. It simply shows you, for the given inputs, I’ll be returning this output. Note that you have to do the parse/stringify thing, or the console will not return the current static cid / change.

That said, when price = 19.5, and cash= 20, you probably don’t want to return ["QUARTER", 1.5] in your change.change , as you currently are.

1 Like

Hey, thanks for your help!
I don’t understand how you get [“QUARTER”, 1.5].

If i run:

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

i get returned with your line of code directly before the return:

for  19.5 ,  20 , and  [ [ 'PENNY', 1.01 ],
  [ 'NICKEL', 2.05 ],
  [ 'DIME', 3.1 ],
  [ 'QUARTER', 3.75 ],
  [ 'ONE', 90 ],
  [ 'FIVE', 55 ],
  [ 'TEN', 20 ],
  [ 'TWENTY', 60 ],
  [ 'ONE HUNDRED', 100 ] ] , returning:  { status: 'OPEN', change: [ [ 'QUARTER', 0.5 ] ] }

and this is exactly how its supposed to be, cid is 2 quarters short and change.change has 2 quarters in it…

Oh wait. You’ve defined change (your returned object) OUTSIDE your checkCashRegister function – so values may be getting carried over. Not saying that’s the WHOLE problem, but scope definitely is going to cause you some issues.

Personally, I’d create the change object right at the outset of the checkCashRegister(), so that it’s pristine each time.

1 Like

Oh my freaking… i didn’t think this could be an issue… but it was… now I was able to submit it. Thank you!

1 Like