# Final project of the intermediate algorithm scripting : cash register function

One of the required output for the following input below is what my problem is about.
The third parameter for the checkCashRegister function is the cid(cash in drawer)
a 2d array and it carries currencies and their respective available amounts. For this particular input the total amount in the cash register \$1.01. The first argument in the function is the cost price and the second argument is the amount payed thus the change due is : amount payed - cost price => 20 - 19.5 = \$0.5. However, the required output as shown below stipulates that the cid has insufficient funds for this input of all the parameters. I canâ€™t seem to figure out the logic since the available amount is in cid is \$1.01 and change due is \$0.5. The cid has an excess of \$0.51 available so it has sufficient amounts and its status should be â€śOPENâ€ť. My code works for all other required outputs except for this one.

`checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])` should return `{status: "INSUFFICIENT_FUNDS", change: []}`

``````function checkCashRegister(price, cash, cid) {

console.log()

//The cidCopy is the copy of the cid;

let cidCopy = [];

let val = 0;

cid.map(item=>

{

val = item;

cidCopy.push(val)

}

);

console.log(cidCopy)

// the roundDown function does exactly as its  name suggests. It rounds down a floating number to a specified precision.

function roundUp(num, decimalPlaces)

{

decimalPlaces = Math.pow(10, decimalPlaces)

return Math.ceil(num * decimalPlaces)/decimalPlaces;

}

//----------------------------------------------------------------

// The currencies variable stores the exact value of the currency

let currencies = [["ONE HUNDRED",100],["TWENTY",20],["TEN",10],["FIVE",5],["ONE",1],["QUARTER",0.25],["DIME",0.1],["NICKEL",0.05],["PENNY",0.01]]

//----------------------------------------------------------------

//the changeArr variable stores total amounts for  each currency received from the cid. The received currencies sum up to the amount equivalent to the change due.

let changeArr = [["ONE HUNDRED",0],["TWENTY",0],["TEN",0],["FIVE",0],["ONE",0],["QUARTER",0],["DIME",0],["NICKEL",0],["PENNY",0]]

//--------------------------------------------------------------------

let change = cash - price;

console.log(`The change due: \$\${change}`)

//---------------------------------------------------------------------

// The totalCid variable stores the total amount of available cash in cid.

let totalCid =cid.reduce((totalCid,item)=>

{

},0)

console.log(`The total cash in the CID: \$\${roundUp(totalCid,2)}`)

//-----------------------------------------------------------------------

// the status variable is the difference value between the totalCid and the change

//that is used to check for the status of the cid.

let status = totalCid - change;

console.log(`The status value is: \$\${roundUp(status,2)}`)

status = roundUp(status,2)

//---------------------------------------------------------------------------

let result = {};

//----------------------------------------------------------------------------

//openChange function assigns assigns values to the the change array for the "OPEN"

//status of the cid.

let openChange = (change) =>

{  if(status>0)

{

let temp = change;

currencies.forEach((item,ind) =>

{

if(temp>=item[1])

{  // CIDINDEX

let cidIndex = 0

cid.forEach((elem,ind)=>

{

if(elem[0] === item[0])

{

cidIndex = ind;

}

},0

)

while(roundUp(temp,3)>=item[1] && cid[cidIndex][1]>=item[1] )

{

// START WHILE LOOP

let curr = roundUp(temp,3)

let changeCurr = temp - item[1];

temp = roundUp(changeCurr,3);

console.log(`curr currecy=> \${item[1]}: current \${curr} vs recieved \${roundUp(changeCurr,2)}`)

console.log(item[0])

console.log(`cid value:[ \${cid[cidIndex]} ] and change value:[ \${changeArr[ind]} ]`)

console.log(cid[cidIndex])

changeArr[ind][1]+=item[1];

cid[cidIndex][1] -= item[1];

console.log(`cid value:[ \${cid[cidIndex]} ] and change value:[ \${changeArr[ind]} ] `)

console.log()

// END OF WHILE LOOP!!!

}

}

}

)

}

}

//---------------------------------------------------------------------------------

// The setStus funtion sets the status of the cid using the status variable and also initializes the result's object properties status and change to the respective values.

let  setStatus = (status)=>

{

if(status < 0)

{

result.status = "INSUFFICIENT_FUNDS";

result.change = [];

}else if(status === 0)

{

result.status = "CLOSED";

result.change = [...cidCopy];

}else if(status>0)

{

result.status = "OPEN";

result.change = changeArr.filter(item=> item[1]>0);
}
}
//-------------------------------------------------------------------------

console.log()
openChange(change)
setStatus(status)
console.log(result)
return result;
}

checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36`

Challenge: Cash Register

The answer is hidden in the denomination of currency available as change.

Letâ€™s step outside of programming for one second and imagine you are working a real cash register.

A customer walks up with \$19.5 worth of purchases and hands you a \$20, you open the register, looking for \$0.50, so perhaps 2 quarters, 5 dimes, 10 nickels, or 50 pennies.

But you find instead a single dollar coin and a penny, meaning you cannot give back the precise amount of \$0.50 to the customer as change. Thus, insufficient funds.

1 Like

It is impossible to hand back \$0.50 from a \$1 bill and a single \$0.01 penny.

I did not fully conceptualize that. Its so subtle. Thank you very much now I can go and try to translate that into code.