How do I exhaustively look for next Available Currency Units from this array CID?

Tell us what’s happening:
I’m trying to move this “while” loop to look for next available “cid” currency to get (balance) change from, but it’s not, after first currencyUnit(e.g. TWENTY). How do I do that?

I have tried an else block with splice method but it seems to be returning an empty array!! :frowning: :frowning: Can anyone help, thanks!!

Your code so far


function checkCashRegister(price, cash, cid) {
              let billsChange;
// return billsChange;
let currencyUnits = {
    PENNY: .01,
    NICKEL: .05,
    DIME: .1,
    QUARTER: .25,
    ONE: 1,
    FIVE: 5,
    TEN: 10,
    TWENTY: 20,
    "ONE HUNDRED": 100
};
let noFunds = { status: "INSUFFICIENT_FUNDS", change: [] };
let closed = { status: "Closed", change: [] };
let open = { status: "Open", change: [] };
let result;
let availableCashInDrawer = 0;
cid.forEach(item => availableCashInDrawer += item[1]);
billsChange = cash - price;
if (billsChange > availableCashInDrawer) {
    result = noFunds;
} else if (billsChange > 0) {
    let units = [];
    let keys = [];
    for (let key in currencyUnits) {
        if (currencyUnits[key] <= billsChange) {
            units.push(currencyUnits[key]);
            keys.push(key);
        }
    }
    units.sort((a, b) => b - a);
    keys.sort((a, b) => b - a);
    // console.log(units, keys)
    cid.forEach(item => {
        if (item[0] === keys[0] && billsChange <= item[1]) {
            console.log(item[0], keys[0]);
            open.change.push([item[0], billsChange]);

        } else if (item[0] === keys[0] && item[1] > 0) {
            while (billsChange >= 0 && item[1] > 0) {
                billsChange -= units[0];
                // item[1] -= units[0];
                // open.change.push([item[0], item[1]]);
            }
            open.change.push([item[0], item[1]]);
        }
    });
    result = open;
} else {
    cid.forEach(item => {
        closed.change.push([item[0], item[1]]);
    });
    result = closed;
}
return result;
}

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 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0.

Challenge: Cash Register

Link to the challenge:

Update on this cid.forEach loop: its going through first available amount but not moving along rest of list to balance out change!!

Am I close or what?? Help, thanks!!!

cid.forEach(item => {
                    if (item[0] === keys[0]) {
                        if (billsChange > 0 && billsChange <= item[1]) {
                            item[1] -= units[0];
                            open.change.push([item[0], billsChange]);
                        } else {

                            while (billsChange > 0) {

                                if (item[1] == 0) {
                                    // open.change.push(keys[0], item[1]);
                                    units.splice(0, 1);
                                    keys.splice(0, 1);

                                    break;
                                }
                                // open.change.push(keys[0], item[1]);
                                // billsChange -= units[0];
                                // item[1] -= units[0];
                                else {
                                    // console.log("here", item[1], billsChange, units[0]);
                                    open.change.push(keys[0], item[1]);
                                    billsChange -= units[0];
                                    item[1] -= units[0];
                                }
                            }
                        }
                    }
                });
                result = open;

Latest update on this::

Now I can go through “cid” array but now it’s calculating something interesting when it goes to “FIVE”. I understand my calculation needs to be adjusted, but I don’t seem to see it how!!

                let tempValues = Object.values(cid);
                tempValues.sort((a, b) => b - a);

                tempValues.forEach(item => {
                    if (item[0] === keys[0] && billsChange < item[1]) {
                        open.change.push([item[0], billsChange]);
                    } else {
                        if (billsChange >= item[1]) {
                            while (item[0] === keys[0] && item[1] > 0) {
                                open.change.push([item[0], item[1]]);
                                billsChange -= units[0];
                                item[1] -= units[0];
                            }
                            units.splice(0, 1);
                            keys.splice(0, 1);
                        }
                    }
                });

Can anyone help me get through this, it clearly needs little bit more attention!! Any feedback is welcome, thanks.

Update on this thread:

I think now it’s showing true nature of what I was looking for, It’s not a total solution but gives you an idea about its mechanics!!

                tempValues.forEach(item => {
                    if (item[0] === keys[0]) {
                        while (billsChange >= units[0] && item[1] > 0) {
                                open.change.push([item[0], item[1]]);
                                billsChange -= units[0];
                                item[1] -= units[0];
                            }
                            units.splice(0, 1);
                            keys.splice(0, 1);
                    } 
                });
  • go through all available currencies
  • adjusts bills change balance
  • inserts how much has been taken from each available currency from register into an returnable object
  • checks which object to return after calculations

I’m keeping this for posterity, whoever might find this useful for their probable solutions, you’re welcome and happy coding :slight_smile: :slight_smile: