Cash Register-Challenge solved but still have some questions

Tell us what’s happening:
Hi, I have cracked the solution and it shares the same logic as the answer provided.

But under this logic, this input:

checkCashRegister(19.7, 20, [[“PENNY”, 0], [“NICKEL”, 0], [“DIME”, 0.30], [“QUARTER”, 0.25], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]])

will return:

{ status: ‘INSUFFICIENT_FUNDS’, change: }

But just by looking at it, we know that we could just return [“DIME”, 0.30].
Anyone has solved this issue?

Sorry my code is a bit messy as I wrote in VSC but not here.

Your code so far

function checkCashRegister(price, cash, cid) {
let change = cash - price; //setup change due

//setup basic currency list
let currency = {'PENNY':0.01, 'NICKEL':0.05,'DIME':0.1,'QUARTER':0.25,'ONE':1,'FIVE':5,'TEN':10,'TWENTY':20,'ONE HUNDRED':100};

//solution of b: change due = total cash
if (cid.reduce((sum,element)=>sum+element[1],0) === change)
  {return {status: "CLOSED", change: cid};}

//solution of ai: total cash < change due
else if (cid.reduce((sum,element)=>sum+element[1],0) < change)
{return {status: "INSUFFICIENT_FUNDS", change: []};}

//solution of aii & c
else {
        let bill = cid.filter(element=>element[1] > 0 && currency[element[0]] <= change); //array containing only existing bill with face value less than change due

        let remval = change; //to be used in the loop
        let changeobj = {}; //to push the required answer into it

        //method to get the desired answer
        for (let j = bill.length -1; j >= 0; j--) //from highest to lowest
              let i; //use to represent quantity needed of each bill/coin
              let facevalue = currency[bill[j][0]]; //the facevalue of each bill.coin
              let iquantity = bill[j][1] / facevalue; //how many bill/coin we have respectively

              for (i = 1; i * facevalue <= remval && i <= iquantity; i++) // to get which bill needed and its total value
                    changeobj[bill[j][0]] = i * facevalue;

              remval -= (i - 1) * facevalue; // to get a new remval for next loop
              remval = Math.round(remval*100) / 100; //to resolve java calculation decimal error
        //to distinguish between solution c & aii
        if (remval > 0) {return {status: "INSUFFICIENT_FUNDS", change: []};} //solution of aii

            else { //solution of c
                    let billnamearr = Object.keys(changeobj); // to get the name & length for next loop
                    let changearr = []; // to put the answer in

                    for (let k = 0; k < billnamearr.length; k++) //make the changearr the format required
                          let temparr = [billnamearr[k],changeobj[billnamearr[k]]];
                    return {status: "OPEN", change: changearr}; //return the answer

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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36.

Challenge: Cash Register

Link to the challenge:

yes, the simplest logic you could build is actually not enough for real life scenarios, and in fact it is not the algorithm used for actual giving change machines.
I have no idea what kind of logic would be needed here, something a bit more complex for sure.
You could try to research this if you want, there is even probably some maths paper somewhere all about this kind of problem.

1 Like