# Cash register challenge

#### peppeferd

Hi everybody, im facing with one javascript challenge.
here is the request:
Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price), payment as the second argument (cash), and cash-in-drawer (cid) as the third argument.

cid is a 2D array listing available currency.

The checkCashRegister() function should always return an object with a status key and a change key.

Return {status: "INSUFFICIENT_FUNDS", change: []} if cash-in-drawer is less than the change due, or if you cannot return the exact change.

Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change if it is equal to the change due.

Otherwise, return {status: "OPEN", change: [...]}, with the change due in coins and bills, sorted in highest to lowest order, as the value of the change key.
__________________________________________________________________________________________________________________
Here is my actual code:

### ​

JavaScript:
``````let array = [["ONE HUNDRED", 100],["TWENTY", 20],["TEN", 10],["FIVE", 5],["ONE", 1],["QUARTER", 0.25],["DIME", 0.1],["NICKEL", 0.05],["PENNY", 0.01]];
function checkCashRegister(price, cash, cid) {
let resto = cash - price;  //x=1355 - 203.54=1151.46
let ratio100 = resto / 100; // 1151.46 / 100 = 11.5146
let pezzi100 = parseInt(ratio100) * 100; //11 * 100=1100
let resto20 = resto - pezzi100; // 1151.46 -1100 = 51.46
let ratio20= resto20 / 20;    //51.46 / 20= 2.573
let pezzi20 = parseInt(ratio20) * 20;  // 2 * 20 = 40
let resto10 = resto - pezzi100 - pezzi20;  //1151.46 - 1100 - 40 =11.46
let ratio10 = resto10 / 10;  //11.46 / 10 = 1.146
let pezzi10 = parseInt(ratio10) * 10;  // 1 * 10 = 10
let resto5 = resto - pezzi100 - pezzi20 - pezzi10;  //1151.46 - 1100 - 40 - 10
let ratio5 = resto5 / 5;
let pezzi5 = parseInt(ratio5) * 5;
let resto1 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5;
let ratio1 = resto1 / 1;
let pezzi1 = parseInt(ratio1) * 1;
let resto025 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5 - pezzi1;
let ratio025 = resto025 / 0.25;
let pezzi025 = parseInt(ratio025) * 0.25
let resto01 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5 - pezzi1 - pezzi025;
let ratio01 = resto01 / 0.1;
let pezzi01 = parseInt(ratio01) * 0.1
let resto005 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5 - pezzi1 - pezzi025 - pezzi01;
let ratio005 = resto005 / 0.05;
let pezzi005 = parseInt(ratio005) * 0.05;
let resto001 = resto - pezzi100 - pezzi20 - pezzi10 - pezzi5 - pezzi1 - pezzi025 - pezzi01 - pezzi005;
let ratio001 = resto001 / 0.01;
let pezzi001 = parseInt(ratio001) * 0.01;

if (price = cash) {
var firstObj = { status: "CLOSED", change: cid}
return firstObj
}
else {
for(let i = 0; i < cid.length; i++) {
for (let j =0; j < cid[i].length; j++) {
var sommaCassa = reduce(cid[j])
if ( sommaCassa > resto && cash < price) {
array[i].pop()
array[0].push(pezzi100)
array[1].push(pezzi20)
array[2].push(pezzi10)
array[3].push(pezzi5)
array[4].push(pezzi1)
array[5].push(pezzi025)
array[6].push(pezzi01)
array[7].push(pezzi005)
array[8].push(pezzi001)
var secondObj = { status: "OPEN", change: array }
return secondObj;
}

}

if (sommaCassa < resto) {
var terzoObj = {status: "INSUFFICIENT_FUNDS", change: cid}
return terzoObj;
}
}}}``````

### ​

#### MeMate

My approach would be something like this
Code:
``````const bills=[[1,'PENNY'],[5,'Nickel'],[10,'DIME'],[25,'QUARTER'],[50,'Half dollar'],[100,'ONE'],[500,'FIVE'],[1000,'TEN'],[2000,'TWENTY'],[10000,'HUNDRED']];
var cid=[[10000,6],[2000,4],[1000,10],[500,4],[100,10],[50,20],[25,50],[10,100],[5,100],[1,260]];
function getBillName(val){
var b=false;
for(var i=0; i<bills.length; i++){
if(bills[i][0]==val){return bills[i][1];}
}
return b;
}
function cashArrayBack(h){
var n=0;
var bills='';
for(var i=0; i<h.length; i++){
var casher=h[i];
n+=casher[0]*casher[1];
bills+=casher[1]+' x '+getBillName(casher[0])+'\t= '+((casher[1]*casher[0])/100)+' USD \n';
}
return [n,bills];
}
function currentCashInRegister(){
return cashArrayBack(cid);
}
function getCashInPennies(ca){
var n=0;
for(var i=0; i<ca.length; i++){
n+=ca[i];
}
return n;
}
function pennyBack(array){
var n=0;
for(var i=0; i<array.length; i++){n+=array[i];}
return n;
}
function takeCash(cu,ch){
var billback=new Array();
var change=ch;
var paid=new Array();
for(var i=0; i<cu.length; i++){
var a=cu[i];
var ident=a[0];
var amount=a[1];
var po=[ident,0];
var pennies=ident*amount;
if(change>0){
var dif=Math.floor(change/ident);
if(dif>0 &&  change>0){
for(var d=0; d<dif; d++){
var newP=change-(ident);
amount--;
if(newP>=0 && amount>=0){
po[1]++;
cu[i]=[ident,amount];
change=newP;
}
}
}
}
if(po[1]){
paid.push(po);
}

}
if(!change){
console.log('GIVE BACK AS CHANGE');
var t=cashArrayBack(paid);
console.log(t[0]/100+' USD in Total\n'+t[1]);
return cu;
}else{
console.log('NOT ENOUGH CHANGE IN REGISTER. '+(change/100)+' USD MISSING');
return false;
}
}
for(var i=0; i<cu.length; i++){
if(cu[i][0]==ca){cu[i][1]++;}
}
return cu;
}
var paid=pennyBack(ca);
var change=(paid-pr);
console.log('CHANGE '+(change/100)+' USD = '+change+' pennies');
if(change>=0){
for(var i=0; i<ca.length; i++){
}
cu=takeCash(cu,change);
if(cu){
cid=cu;
return 'OKAY CHANGE SHOULD BE '+(change/100)+' USD';
}else{
return 'Sorry, cash register ran out of change';
}
}else{
return 'Hey Bagger Vance!! '+Math.abs(change/100)+' Dollars more for this!!!!';
}
}

function purchaseSomething(price,cash){

console.log('----------------------------------------------------------------------------\nBuy something for '+(price/100)+' USD and pay with '+(pennyBack(cash)/100)+' USD');
for(var i=0; i<cash.length; i++){
console.log(getBillName(cash[i])+' USD BILL ');
}

console.log('STATUS '+status);
}

console.log(bills);
console.log(cid);
console.log('Bills and coins in cash register');
var cus=currentCashInRegister();
console.log(cus[0]/100+' USD in Total\n'+cus[1]);

purchaseSomething(5128,[10000]);
console.log('Bills and coins in cash register');
cus=currentCashInRegister();
console.log(cus[0]/100+' USD in Total\n'+cus[1]);

purchaseSomething(150,[10,10,100,25,10]);
console.log('Bills and coins in cash register');
cus=currentCashInRegister();
console.log(cus[0]/100+' USD in Total\n'+cus[1]);

purchaseSomething(53,[2000,2000,2000,2000,2000]);
console.log('Bills and coins in cash register');
cus=currentCashInRegister();
console.log(cus[0]/100+' USD in Total\n'+cus[1]);

purchaseSomething(5128,[2000,2000,2000]);
console.log('Bills and coins in cash register');
cus=currentCashInRegister();
console.log(cus[0]/100+' USD in Total\n'+cus[1]);

purchaseSomething(9999,[2000,2000,2000,2000,2000]);
console.log('Bills and coins in cash register');
cus=currentCashInRegister();
console.log(cus[0]/100+' USD in Total\n'+cus[1]);

purchaseSomething(5128,[100]);
console.log('Bills and coins in cash register');
cus=currentCashInRegister();
console.log(cus[0]/100+' USD in Total\n'+cus[1]);

purchaseSomething(5128,[10000]);
console.log('Bills and coins in cash register');
cus=currentCashInRegister();
console.log(cus[0]/100+' USD in Total\n'+cus[1]);

purchaseSomething(1,[10000]);
console.log('Bills and coins in cash register');
cus=currentCashInRegister();
console.log(cus[0]/100+' USD in Total\n'+cus[1]);

purchaseSomething(5128,[10000]);
console.log('Bills and coins in cash register');
cus=currentCashInRegister();
console.log(cus[0]/100+' USD in Total\n'+cus[1]);

purchaseSomething(5128,[10000]);
console.log('Bills and coins in cash register');
cus=currentCashInRegister();
console.log(cus[0]/100+' USD in Total\n'+cus[1]);``````
purchaseSomething expects to arguments
1) Price in Pennies
2) Array of Bills (e.g, [100,50,10] for [DOLLAR,HALF DOLLAR,DIME]

It will return the change sorted by type and sort of bill (paper or metal)
Also it tries to keep small change as long as possible.

#### peppeferd

Thank you, after i did it by my own.

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

### Members online

No members online now.